예제 #1
0
        /// <summary>
        /// Gets Bearer token from Authorization code and manually passing Bearer Token url
        /// </summary>
        /// <param name="tokenEndpoint"></param>
        /// <param name="code"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <TokenResponse> GetBearerTokenAsync(string tokenEndpoint, string code, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(tokenEndpoint))
            {
                AdvancedLogger.Log("BearerToken Endpoint is empty.");
                return(new TokenResponse(HttpStatusCode.InternalServerError, "BearerToken Endpoint is empty."));
            }

            AdvancedLoggerEnabled = true;
            //Set internal property to track only informational -intuit_tid based logs
            if (EnableAdvancedLoggerInfoMode == true)
            {
                ShowInfoLogs = true;
            }

            if (this.CustomLogger != null)
            {
                //Use custom logger
                AdvancedLogger = LogHelper.GetAdvancedLoggingCustom(this.CustomLogger);
            }
            else
            {
                //Intialize Logger
                AdvancedLogger = LogHelper.GetAdvancedLogging(enableSerilogRequestResponseLoggingForDebug: this.EnableSerilogRequestResponseLoggingForDebug, enableSerilogRequestResponseLoggingForTrace: this.EnableSerilogRequestResponseLoggingForTrace, enableSerilogRequestResponseLoggingForConsole: this.EnableSerilogRequestResponseLoggingForConsole, enableSerilogRequestResponseLoggingForFile: this.EnableSerilogRequestResponseLoggingForFile, serviceRequestLoggingLocationForFile: this.ServiceRequestLoggingLocationForFile);
            }

            var tokenClient = new TokenClient(tokenEndpoint, ClientID, ClientSecret);

            return(await tokenClient.RequestTokenFromCodeAsync(code, RedirectURI, cancellationToken : cancellationToken).ConfigureAwait(false));
        }
예제 #2
0
        ///// <summary>
        ///// Get Authorization Url
        ///// </summary>
        ///// <param name="scopes"></param>
        ///// <param name="CSRFToken"></param>
        ///// <param name="getRealmId"></param>
        ///// <returns></returns>
        //public string GetAuthorizationURL(List<OidcScopes> scopes, string CSRFToken, bool getRealmId )
        //{
        //    string scopeValue = "";
        //    string realmIdJson = OidcConstants.AuthorizeRequest.IdToken_ReamId;
        //    string authorizationRequest = "";
        //    for (var index = 0; index < scopes.Count; index++)
        //    {
        //        scopeValue += scopes[index].GetStringValue() + " ";
        //    }
        //    scopeValue = scopeValue.TrimEnd();
        //    this.CSRFToken = CSRFToken;
        //    if (getRealmId == true)
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}&claims={5}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            Uri.EscapeDataString(realmIdJson),
        //            CSRFToken);
        //    }
        //    else
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            CSRFToken);
        //    }
        //    return authorizationRequest;
        //}

        ///// <summary>
        ///// Get Authorization Url
        ///// </summary>
        ///// <param name="scopes"></param>
        ///// <param name="CSRFToken"></param>
        ///// <param name="getRealmId"></param>
        ///// <returns></returns>
        //public string GetAuthorizationURL(List<OidcScopes> scopes, bool getRealmId)
        //{
        //    string scopeValue = "";
        //    string realmIdJson = OidcConstants.AuthorizeRequest.IdToken_ReamId;
        //    string authorizationRequest = "";
        //    for (var index = 0; index < scopes.Count; index++)
        //    {
        //        scopeValue += scopes[index].GetStringValue() + " ";
        //    }
        //    scopeValue = scopeValue.TrimEnd();
        //    this.CSRFToken = GenerateCSRFToken();
        //    if (getRealmId == true)
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}&claims={5}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            Uri.EscapeDataString(realmIdJson),
        //            CSRFToken);
        //    }
        //    else
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            CSRFToken);
        //    }
        //    return authorizationRequest;
        //}


        ///// <summary>
        ///// Validates ID token
        ///// </summary>
        ///// <param name="idToken"></param>
        ///// <returns></returns>
        //public string GetRealmIdFromIDTokenAsync(string idToken)
        //{

        //    if (idToken != null)
        //    {
        //        string[] splitValues = idToken.Split('.');


        //        if (splitValues[1] != null)
        //        {
        //            var payloadJson = Encoding.UTF8.GetString(Base64Url.Decode(splitValues[1].ToString()));
        //            IdTokenJWTClaimTypes payloadData = JsonConvert.DeserializeObject<IdTokenJWTClaimTypes>(payloadJson);

        //            if (payloadData.RealmId != null)
        //            {
        //                return payloadData.RealmId.ToString();
        //            }

        //        }

        //    }

        //    return null;

        //}

        #endregion



        /// <summary>
        /// Gets Bearer token from Authorization code
        /// </summary>
        /// <param name="code"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <TokenResponse> GetBearerTokenAsync(string code, CancellationToken cancellationToken = default(CancellationToken))
        {
            //Intialize Logger
            AdvancedLogger = LogHelper.GetAdvancedLogging(enableSerilogRequestResponseLoggingForDebug: this.EnableSerilogRequestResponseLoggingForDebug, enableSerilogRequestResponseLoggingForTrace: this.EnableSerilogRequestResponseLoggingForTrace, enableSerilogRequestResponseLoggingForConsole: this.EnableSerilogRequestResponseLoggingForConsole, enableSerilogRequestResponseLoggingForRollingFile: this.EnableSerilogRequestResponseLoggingForRollingFile, serviceRequestLoggingLocationForFile: this.ServiceRequestLoggingLocationForFile);

            var tokenClient = new TokenClient(DiscoveryDoc.TokenEndpoint, ClientID, ClientSecret);

            return(await tokenClient.RequestTokenFromCodeAsync(code, RedirectURI, cancellationToken : cancellationToken).ConfigureAwait(false));
        }
        ///// <summary>
        ///// Get Authorization Url
        ///// </summary>
        ///// <param name="scopes"></param>
        ///// <param name="CSRFToken"></param>
        ///// <param name="getRealmId"></param>
        ///// <returns></returns>
        //public string GetAuthorizationURL(List<OidcScopes> scopes, string CSRFToken, bool getRealmId )
        //{
        //    string scopeValue = "";
        //    string realmIdJson = OidcConstants.AuthorizeRequest.IdToken_ReamId;
        //    string authorizationRequest = "";
        //    for (var index = 0; index < scopes.Count; index++)
        //    {
        //        scopeValue += scopes[index].GetStringValue() + " ";
        //    }
        //    scopeValue = scopeValue.TrimEnd();
        //    this.CSRFToken = CSRFToken;
        //    if (getRealmId == true)
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}&claims={5}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            Uri.EscapeDataString(realmIdJson),
        //            CSRFToken);
        //    }
        //    else
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            CSRFToken);
        //    }
        //    return authorizationRequest;
        //}

        ///// <summary>
        ///// Get Authorization Url
        ///// </summary>
        ///// <param name="scopes"></param>
        ///// <param name="CSRFToken"></param>
        ///// <param name="getRealmId"></param>
        ///// <returns></returns>
        //public string GetAuthorizationURL(List<OidcScopes> scopes, bool getRealmId)
        //{
        //    string scopeValue = "";
        //    string realmIdJson = OidcConstants.AuthorizeRequest.IdToken_ReamId;
        //    string authorizationRequest = "";
        //    for (var index = 0; index < scopes.Count; index++)
        //    {
        //        scopeValue += scopes[index].GetStringValue() + " ";
        //    }
        //    scopeValue = scopeValue.TrimEnd();
        //    this.CSRFToken = GenerateCSRFToken();
        //    if (getRealmId == true)
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}&claims={5}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            Uri.EscapeDataString(realmIdJson),
        //            CSRFToken);
        //    }
        //    else
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            CSRFToken);
        //    }
        //    return authorizationRequest;
        //}


        ///// <summary>
        ///// Validates ID token
        ///// </summary>
        ///// <param name="idToken"></param>
        ///// <returns></returns>
        //public string GetRealmIdFromIDTokenAsync(string idToken)
        //{

        //    if (idToken != null)
        //    {
        //        string[] splitValues = idToken.Split('.');


        //        if (splitValues[1] != null)
        //        {
        //            var payloadJson = Encoding.UTF8.GetString(Base64Url.Decode(splitValues[1].ToString()));
        //            IdTokenJWTClaimTypes payloadData = JsonConvert.DeserializeObject<IdTokenJWTClaimTypes>(payloadJson);

        //            if (payloadData.RealmId != null)
        //            {
        //                return payloadData.RealmId.ToString();
        //            }

        //        }

        //    }

        //    return null;

        //}

        #endregion



        /// <summary>
        /// Gets Bearer token from Authorization code
        /// </summary>
        /// <param name="code"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <TokenResponse> GetBearerTokenAsync(string code, CancellationToken cancellationToken = default(CancellationToken))
        {
            AdvancedLoggerEnabled = true;
            //Set internal property to track only informational -intuit_tid based logs
            if (EnableAdvancedLoggerInfoMode == true)
            {
                ShowInfoLogs = true;
            }
            //Intialize Logger
            AdvancedLogger = LogHelper.GetAdvancedLogging(enableSerilogRequestResponseLoggingForDebug: this.EnableSerilogRequestResponseLoggingForDebug, enableSerilogRequestResponseLoggingForTrace: this.EnableSerilogRequestResponseLoggingForTrace, enableSerilogRequestResponseLoggingForConsole: this.EnableSerilogRequestResponseLoggingForConsole, enableSerilogRequestResponseLoggingForRollingFile: this.EnableSerilogRequestResponseLoggingForRollingFile, serviceRequestLoggingLocationForFile: this.ServiceRequestLoggingLocationForFile);



            var tokenClient = new TokenClient(DiscoveryDoc.TokenEndpoint, ClientID, ClientSecret);

            return(await tokenClient.RequestTokenFromCodeAsync(code, RedirectURI, cancellationToken : cancellationToken).ConfigureAwait(false));
        }
예제 #4
0
        ///// <summary>
        ///// Get Authorization Url
        ///// </summary>
        ///// <param name="scopes"></param>
        ///// <param name="CSRFToken"></param>
        ///// <param name="getRealmId"></param>
        ///// <returns></returns>
        //public string GetAuthorizationURL(List<OidcScopes> scopes, string CSRFToken, bool getRealmId )
        //{
        //    string scopeValue = "";
        //    string realmIdJson = OidcConstants.AuthorizeRequest.IdToken_ReamId;
        //    string authorizationRequest = "";
        //    for (var index = 0; index < scopes.Count; index++)
        //    {
        //        scopeValue += scopes[index].GetStringValue() + " ";
        //    }
        //    scopeValue = scopeValue.TrimEnd();
        //    this.CSRFToken = CSRFToken;
        //    if (getRealmId == true)
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}&claims={5}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            Uri.EscapeDataString(realmIdJson),
        //            CSRFToken);
        //    }
        //    else
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            CSRFToken);
        //    }
        //    return authorizationRequest;
        //}

        ///// <summary>
        ///// Get Authorization Url
        ///// </summary>
        ///// <param name="scopes"></param>
        ///// <param name="CSRFToken"></param>
        ///// <param name="getRealmId"></param>
        ///// <returns></returns>
        //public string GetAuthorizationURL(List<OidcScopes> scopes, bool getRealmId)
        //{
        //    string scopeValue = "";
        //    string realmIdJson = OidcConstants.AuthorizeRequest.IdToken_ReamId;
        //    string authorizationRequest = "";
        //    for (var index = 0; index < scopes.Count; index++)
        //    {
        //        scopeValue += scopes[index].GetStringValue() + " ";
        //    }
        //    scopeValue = scopeValue.TrimEnd();
        //    this.CSRFToken = GenerateCSRFToken();
        //    if (getRealmId == true)
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}&claims={5}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            Uri.EscapeDataString(realmIdJson),
        //            CSRFToken);
        //    }
        //    else
        //    {
        //        authorizationRequest = string.Format("{0}?client_id={1}&response_type=code&scope={2}&redirect_uri={3}&state={4}",
        //            DiscoveryDoc.AuthorizeEndpoint,
        //            ClientID,
        //            Uri.EscapeDataString(scopeValue),
        //            Uri.EscapeDataString(RedirectURI),
        //            CSRFToken);
        //    }
        //    return authorizationRequest;
        //}


        ///// <summary>
        ///// Validates ID token
        ///// </summary>
        ///// <param name="idToken"></param>
        ///// <returns></returns>
        //public string GetRealmIdFromIDTokenAsync(string idToken)
        //{

        //    if (idToken != null)
        //    {
        //        string[] splitValues = idToken.Split('.');


        //        if (splitValues[1] != null)
        //        {
        //            var payloadJson = Encoding.UTF8.GetString(Base64Url.Decode(splitValues[1].ToString()));
        //            IdTokenJWTClaimTypes payloadData = JsonConvert.DeserializeObject<IdTokenJWTClaimTypes>(payloadJson);

        //            if (payloadData.RealmId != null)
        //            {
        //                return payloadData.RealmId.ToString();
        //            }

        //        }

        //    }

        //    return null;

        //}

        #endregion



        /// <summary>
        /// Gets Bearer token from Authorization code
        /// </summary>
        /// <param name="code"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <TokenResponse> GetBearerTokenAsync(string code, CancellationToken cancellationToken = default(CancellationToken))
        {
            var tokenClient = new TokenClient(DiscoveryDoc.TokenEndpoint, ClientID, ClientSecret);

            return(await tokenClient.RequestTokenFromCodeAsync(code, RedirectURI, cancellationToken : cancellationToken).ConfigureAwait(false));
        }