Esempio n. 1
0
 /// <summary>
 /// This method will end the user's Okta session in the browser.
 /// </summary>
 /// <param name="stateManager">The state manager associated with the login that you wish to log out</param>
 /// <returns>Task which tracks the progress of the logout</returns>
 public Task <OktaState> SignOutOfOktaAsync(OktaState stateManager)
 {
     validator.Validate(this.Config);
     this.currentTask = new TaskCompletionSource <OktaState>();
     if (!stateManager.IsAuthenticated)
     {
         this.currentTask.SetResult(stateManager);
         return(this.currentTask.Task);
     }
     this.GenerateStateCodeVerifierAndChallenge();
     loggingOutClientsByState.Add(this.State, this);
     this.LaunchBrowser(this.GenerateLogoutUrl(new LogoutOptions(stateManager, this.Config, this.State)));
     return(currentTask.Task);
 }
Esempio n. 2
0
        /// <summary>
        /// Exchange authorization code for an access token
        /// </summary>
        /// <param name="code">The authorization code received from the login</param>
        /// <returns>A Task which is complete when the login flow is completed.  The actual return value <see cref="OktaState"/> or <see cref="OAuthException"/> is returned to the original Task returned from <see cref="SignInWithBrowserAsync"/>.</returns>
        private async Task ExchangeAuthCodeForToken(string code)
        {
            List <KeyValuePair <string, string> > kvdata = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("grant_type", "authorization_code"),
                new KeyValuePair <string, string>("code", code),
                new KeyValuePair <string, string>("redirect_uri", this.Config.RedirectUri),
                new KeyValuePair <string, string>("client_id", this.Config.ClientId),
                new KeyValuePair <string, string>("code_verifier", CodeVerifier)
            };
            var content = new FormUrlEncodedContent(kvdata);

            var request = new HttpRequestMessage(HttpMethod.Post, this.Config.GetAccessTokenUrl())
            {
                Content = content, Method = HttpMethod.Post
            };
            HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);

            string text = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            Dictionary <string, string> data = Helpers.JsonDecode(text);

            if (data.ContainsKey("error"))
            {
                currentTask.SetException(new OAuthException()
                {
                    ErrorTitle       = data["error"],
                    ErrorDescription = data.GetValueOrDefault("error_description"),
                    RequestUrl       = this.Config.GetAccessTokenUrl(),
                    ExtraData        = kvdata
                });

                return;
            }

            // TODO: add a StateManager constructor that takes Dictionary<string, string>
            OktaState stateManager = new OktaState(
                data["access_token"],
                data["token_type"],
                data.GetValueOrDefault("id_token"),
                data.GetValueOrDefault("refresh_token"),
                data.ContainsKey("expires_in") ? (int?)(int.Parse(data["expires_in"])) : null,
                data.GetValueOrDefault("scope") ?? this.Config.Scope);

            currentTask.SetResult(stateManager);
        }
 public LogoutOptions(OktaState stateManager, IOktaConfig oktaConfig, string state)
 {
     this.IdTokenHint           = stateManager.IdToken;
     this.PostLogoutRedirectUri = oktaConfig.PostLogoutRedirectUri;
     this.State = state;
 }