예제 #1
0
        public AzureADAuthRestResponse <AccessTokenClass, OAuthError> GetAccessToken_UserCredential(DatabaseManager dbmanager)
        {
            StateManager        _stateIdManager = new StateManager(Config.LoggedInUserEmail, Config.Scope);
            LocalStorageManager _localStorage   = new LocalStorageManager(Config, dbmanager);
            string _stateId = _stateIdManager.GetStateIdForCurrentUser(dbmanager);
            var    resp     = new AzureADAuthRestResponse <AccessTokenClass, OAuthError>();
            // try to look for the available token first
            // if token not available then use authorize code to download it
            // if token expires then download new token using refresh token and store in database
            // if authorize token not available send send the response then auhorize token not available
            AccessTokenClass token = _localStorage.GetAccessToken(_stateId);

            if (token == null)
            {
                // check if authcode available
                AuthCode authcode = _localStorage.GetAuthCode(_stateId);
                if (authcode == null)
                {
                    resp.OAuthError = OAuthErrors.AuthCodeNotFound;
                    return(resp);
                }
                var res = GetAccessToken(authcode.code, TokenRetrivalType.AuthorizationCode);
                // check for any error in response
                if (res.IsSucceeded)
                {
                    token = res.Result;
                    _localStorage.AddAccessToken(_stateId, token);
                    // add token into database
                }
                else
                {
                    // send status to front page that it is not authorized, authorization code has expired
                    resp.OAuthError = OAuthErrors.AuthCodeExpires;
                    return(resp);
                }
            }
            // check for token's validity, if expires then recalculate from refresh token
            if (!validateToken(token))
            {
                var res = GetAccessToken(token.RefreshToken, TokenRetrivalType.RefreshToken);
                // check for any error, if error is not present then cast to
                if (res.IsSucceeded)
                {
                    token = res.Result;
                    _localStorage.UpdateAccessToken(_stateId, token);
                }
            }
            resp.Result     = token;
            resp.OAuthError = OAuthErrors.None;
            return(resp);
        }
예제 #2
0
        public AzureADAuthRestResponse <AccessTokenClass, OAuthError> GetAccessToken(string code, TokenRetrivalType tokenRetrivalType)
        {
            RestClient  _client  = new RestClient(GetEndPoint(EndPointType.authorize));
            RestRequest _request = new RestRequest();

            _request = new RestRequest(Method.POST);
            _request.AddParameter("client_id", Config.ClientId);
            _request.AddParameter("scope", Config.Scope);

            if (tokenRetrivalType == TokenRetrivalType.AuthorizationCode)
            {
                _request.AddParameter("code", code);
                _request.AddParameter("grant_type", "authorization_code");
            }
            else if (tokenRetrivalType == TokenRetrivalType.RefreshToken)
            {
                _request.AddParameter("refresh_token", code);
                _request.AddParameter("grant_type", "refresh_token");
            }

            _request.AddParameter("redirect_uri", Config.RedirectURL);

            _request.AddParameter("client_secret", Config.ClientSecret);

            IRestResponse response = _client.Execute(_request);
            AzureADAuthRestResponse <AccessTokenClass, OAuthError> resp = new AzureADAuthRestResponse <AccessTokenClass, OAuthError>();

            if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                OAuthError error = JsonConvert.DeserializeObject <OAuthError>(response.Content, Converter.Settings);
                resp.Error       = error;
                resp.IsSucceeded = false;
            }
            else
            {
                AccessTokenClass result = JsonConvert.DeserializeObject <AccessTokenClass>(response.Content, Converter.Settings);
                resp.Result      = result;
                resp.IsSucceeded = true;
            }
            return(resp);
        }
예제 #3
0
        public AzureADAuthRestResponse <AccessTokenClass, OAuthError> GetAccessToken_FromClientCredential(string clientId, string resource, string clientSecret, string scope)
        {
            Config.ClientId     = clientId;
            Config.Resource     = resource;
            Config.ClientSecret = clientSecret;
            Config.Scope        = scope;


            RestClient  _client  = new RestClient();
            RestRequest _request = new RestRequest();

            _client.BaseUrl = new Uri(GetEndPoint(EndPointType.token));
            _request.Parameters.Clear();
            _request.Method = Method.POST;
            _request.AddParameter("client_id", Config.ClientId);
            _request.AddParameter("grant_type", "client_credentials");
            _request.AddParameter("resource", Config.Resource);
            _request.AddParameter("client_secret", Config.ClientSecret);
            _request.AddParameter("scope", Config.Scope);

            var response = _client.Execute(_request);
            AzureADAuthRestResponse <AccessTokenClass, OAuthError> resp = new AzureADAuthRestResponse <AccessTokenClass, OAuthError>();

            if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                OAuthError error = JsonConvert.DeserializeObject <OAuthError>(response.Content, Converter.Settings);
                resp.Error       = error;
                resp.IsSucceeded = false;
            }
            else
            {
                AccessTokenClass result = JsonConvert.DeserializeObject <AccessTokenClass>(response.Content, Converter.Settings);
                resp.Result      = result;
                resp.IsSucceeded = true;
            }

            return(resp);
        }