Example #1
0
        /// <summary>
        /// Refreshs the token.
        /// </summary>
        /// <param name="token">Token.</param>
        public OauthTokenResponse RefreshToken(string token, string username, bool store = true)
        {
            var            scope      = Scopes.Replace(",", "%20");
            string         url        = string.Format("{0}", TokenEndpoint);
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

            webRequest.Method      = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            if (!string.IsNullOrEmpty(AppSettings["ProxyHost"]))
            {
                webRequest.Proxy = GetWebRequestProxy();
            }

            var dataStr = string.Format("grant_type=refresh_token&refresh_token={0}&scope={1}", token, scope);

            if (this.UseAuthorizationHeader)
            {
                webRequest.Headers.Add(HttpRequestHeader.Authorization, GetBasicAuthenticationSecret());
            }
            else
            {
                dataStr += string.Format("&client_id={0}&client_secret={1}", this.ClientId, this.ClientSecret);
            }

            byte[] data = System.Text.Encoding.UTF8.GetBytes(dataStr);

            webRequest.ContentLength = data.Length;

            using (var requestStream = webRequest.GetRequestStream()) {
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                try {
                    using (var httpResponse = (HttpWebResponse)webRequest.GetResponse()) {
                        using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
                            var result   = streamReader.ReadToEnd();
                            var response = JsonSerializer.DeserializeFromString <OauthTokenResponse>(result);

                            if (store)
                            {
                                DBCookie.DeleteDBCookies(Context, HttpContext.Current.Session.SessionID);
                                StoreTokenAccess(response.access_token, username, response.expires_in);
                                StoreTokenRefresh(response.refresh_token, username);
                                if (!string.IsNullOrEmpty(response.id_token))
                                {
                                    StoreTokenId(response.id_token, username, response.expires_in);
                                }
                            }
                            return(response);
                        }
                    }
                } catch (Exception e) {
                    Context.LogError(this, "RefreshToken error : " + e.Message);
                    RevokeSessionCookies();
                    throw e;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Gets the authorization URL.
        /// </summary>
        /// <returns>The authorization URL.</returns>
        public string GetAuthorizationUrl()
        {
            if (string.IsNullOrEmpty(AuthEndpoint))
            {
                throw new Exception("Invalid Authorization endpoint");
            }

            var scope        = Scopes.Replace(",", "%20");
            var redirect_uri = HttpUtility.UrlEncode(Callback);
            var query        = string.Format("response_type={0}&scope={1}&client_id={2}&state={3}&redirect_uri={4}&nonce={5}",
                                             "code", scope, ClientId, Guid.NewGuid().ToString(), redirect_uri, Guid.NewGuid().ToString());

            string url = string.Format("{0}?{1}", AuthEndpoint, query);

            return(url);
        }
Example #3
0
        /// <summary>
        /// Accesses the token.
        /// </summary>
        /// <param name="code">Code.</param>
        public OauthTokenResponse AccessToken(string code)
        {
            Context.LogDebug(this, "AccessToken - " + HttpContext.Current.Session.SessionID);
            var            scope      = Scopes.Replace(",", "%20");
            string         url        = string.Format("{0}", TokenEndpoint);
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

            webRequest.Method      = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            if (!string.IsNullOrEmpty(AppSettings["ProxyHost"]))
            {
                webRequest.Proxy = GetWebRequestProxy();
            }

            var dataStr = string.Format("grant_type=authorization_code&redirect_uri={0}&code={1}&scope={2}", HttpUtility.UrlEncode(Callback), code, scope);

            if (this.UseAuthorizationHeader)
            {
                webRequest.Headers.Add(HttpRequestHeader.Authorization, GetBasicAuthenticationSecret());
            }
            else
            {
                dataStr += string.Format("&client_id={0}&client_secret={1}", this.ClientId, this.ClientSecret);
            }

            byte[] data = System.Text.Encoding.UTF8.GetBytes(dataStr);

            webRequest.ContentLength = data.Length;

            using (var requestStream = webRequest.GetRequestStream()) {
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                try {
                    using (var httpResponse = (HttpWebResponse)webRequest.GetResponse()) {
                        using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
                            var result   = streamReader.ReadToEnd();
                            var response = JsonSerializer.DeserializeFromString <OauthTokenResponse>(result);
                            if (response.access_token != null)
                            {
                                StoreTokenAccess(response.access_token, "", response.expires_in);
                            }
                            if (response.refresh_token != null)
                            {
                                StoreTokenRefresh(response.refresh_token, "");
                            }
                            if (response.id_token != null)
                            {
                                StoreTokenId(response.id_token, "", response.expires_in);
                            }
                            Context.LogDebug(this, "Access Token valid " + response.expires_in + " seconds");
                            return(response);
                        }
                    }
                } catch (Exception e) {
                    DeleteTokenAccess();
                    DeleteTokenRefresh();
                    DeleteTokenId();
                    throw e;
                }
            }
        }