Example #1
0
        public string GetAuthorizationCodeRequestUrl(OAuth2Credential credential, OAuth2TokenRequestSettings requestSettings)
        {
            if (credential == null)
            {
                throw new ArgumentNullException(nameof(credential));
            }
            if (requestSettings == null)
            {
                throw new ArgumentNullException(nameof(requestSettings));
            }
            if (!credential.RedirectUrls.Contains(requestSettings.RedirectUri))
            {
                throw new ArgumentException("RedirectUri must match one of the values in credential.RedirectUrls", nameof(requestSettings));
            }

            var queryParams = new Dictionary <string, string>();

            queryParams.Add("response_type", "code");
            queryParams.Add("client_id", credential.ClientId);
            queryParams.Add("redirect_uri", requestSettings.RedirectUri);
            queryParams.Add("scope", string.Join(" ", requestSettings.Scopes));
            queryParams.Add("state", requestSettings.State);
            if (!requestSettings.IsOnlineAccess)
            {
                queryParams.Add("access_type", "offline");
            }
            if (requestSettings.Prompts.Count > 0)
            {
                if (requestSettings.Prompts.Contains(OAuth2TokenRequestPrompt.None))
                {
                    queryParams.Add("prompt", "none");
                }
                else
                {
                    var prompts = new List <string>();
                    foreach (var prompt in requestSettings.Prompts)
                    {
                        switch (prompt)
                        {
                        case OAuth2TokenRequestPrompt.Consent: prompts.Add("consent"); break;

                        case OAuth2TokenRequestPrompt.SelectAccount: prompts.Add("select_account"); break;
                        }
                    }
                    queryParams.Add("prompt", string.Join(" ", prompts));
                }
            }
            if (!string.IsNullOrEmpty(requestSettings.LoginHint))
            {
                queryParams.Add("login_hint", requestSettings.LoginHint);
            }
            if (requestSettings.IncludeGrantedScopes)
            {
                queryParams.Add("include_granted_scopes", "true");
            }

            string queryString = _net.GetQueryString(queryParams);

            return($"{AuthorizationEndpoint}?{queryString}");
        }
Example #2
0
        public async Task <OAuth2TokenInfo> GetTokenInfoAsync(OAuth2Credential credential, string redirectUri, string authorizationCode)
        {
            var queryParams = new Dictionary <string, string>();

            queryParams.Add("code", authorizationCode);
            queryParams.Add("client_id", credential.ClientId);
            queryParams.Add("client_secret", credential.ClientSecret);
            queryParams.Add("redirect_uri", redirectUri);
            queryParams.Add("grant_type", "authorization_code");
            var queryString = _net.GetQueryString(queryParams);

            var request = _net.CreateHttp(TokenEndpoint);

            request.Method = "POST";
            request.Headers[HttpRequestHeader.ContentType]   = "application/x-www-form-urlencoded";
            request.Headers[HttpRequestHeader.ContentLength] = Encoding.UTF8.GetByteCount(queryString).ToString();
            using (var stream = await request.GetRequestStreamAsync().ConfigureAwait(false))
                using (var writer = new StreamWriter(stream))
                {
                    await writer.WriteAsync(queryString).ConfigureAwait(false);
                }

            DateTime issuedTime = DateTime.UtcNow;

            using (var response = await request.GetResponseAsync().ConfigureAwait(false))
            {
                var jsonContent = await _net.GetResponseTextAsync(response).ConfigureAwait(false);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var tokenInfo = OAuth2TokenInfo.CreateFromJson(jsonContent);
                    tokenInfo.IssuedTime = issuedTime;
                    return(tokenInfo);
                }
                else
                {
                    throw new WebException(jsonContent, WebExceptionStatus.ReceiveFailure);
                }
            }
        }