Ejemplo n.º 1
0
        public async Task <ActionResult> FinishAuth()
        {
            UpdateOAuth2Service(ClientSettings);
            string code   = Request.QueryString["code"];
            var    result = await _oAuth2Service.FinishAuthorization(code);

            ClientSettings.UpdateAndSave(result);

            return(RedirectToAction("DisplayInformation", new RouteValueDictionary(result)));
        }
        public async Task <IActionResult> AuthorizeAsync([FromQuery] string code, [FromQuery] string scope, [FromQuery] string state)
        {
            var contextKey  = $"{nameof(DigikeyApi)}-{User.Identity.Name}";
            var authRequest = ServerContext.Get <OAuthAuthorization>(contextKey);

            if (authRequest == null)
            {
                // try get next integration
                contextKey  = $"{nameof(MouserApi)}-{User.Identity.Name}";
                authRequest = ServerContext.Get <OAuthAuthorization>(contextKey);
            }
            if (authRequest != null)
            {
                var authResult = await _oAuth2Service.FinishAuthorization(code);

                // store the authorization tokens
                // result.AccessToken
                // result.RefreshToken
                // result.ExpiresIn
                authRequest.AuthorizationReceived = true;
                if (authResult.IsError)
                {
                    authRequest.Error            = authResult.Error;
                    authRequest.ErrorDescription = authResult.ErrorDescription;
                }
                else
                {
                    authRequest.AccessToken  = authResult.AccessToken;
                    authRequest.RefreshToken = authResult.RefreshToken;
                    authRequest.CreatedUtc   = DateTime.UtcNow;
                    authRequest.ExpiresUtc   = DateTime.UtcNow.Add(TimeSpan.FromSeconds(authResult.ExpiresIn));
                }

                ServerContext.Set(contextKey, authRequest);
                // save the credential
                await _credentialService.SaveOAuthCredentialAsync(new Common.Models.OAuthCredential
                {
                    Provider       = contextKey,
                    AccessToken    = authRequest.AccessToken,
                    RefreshToken   = authRequest.RefreshToken,
                    DateCreatedUtc = authRequest.CreatedUtc,
                    DateExpiresUtc = authRequest.ExpiresUtc,
                });

                return(Redirect(authRequest.ReturnToUrl));
            }
            return(BadRequest("No authorization request found, invalid callback."));
        }