Пример #1
0
    public IOAuthClientResponse CreateAuthorizationCodeWithPkceRedirect(IEnumerable <string> scopes = null, string state = null)
    {
        var codeVerifier = PkceUtils.GenerateCodeVerifier();

        var queryStringParams = new Dictionary <string, string>
        {
            { Common.ResponseType, ResponseTypes.Code },
            { Common.ClientId, _configuration.ClientId },
            { Common.RedirectUri, _configuration.RedirectUri },
            { Common.State, state ?? StateUtils.Generate() },
            { Common.CodeChallenge, PkceUtils.GenerateCodeChallenge(codeVerifier) },
            { Common.CodeChallengeMethod, CodeChallengeMethodTypes.S256 }
        }.AddScopes(scopes);

        var redirectUri = QueryHelpers.AddQueryString(_configuration.AuthorizeEndpoint, queryStringParams);

        var response = new OAuthRedirect
        {
            Uri          = redirectUri,
            State        = state,
            CodeVerifier = codeVerifier
        };

        return(response);
    }
    public async Task <IActionResult> Index(OAuthTesterViewModel oAuthTesterViewModel)
    {
        var state = StateUtils.Generate();
        var oAuthClientConfiguration = oAuthTesterViewModel.OAuthClientConfiguration;
        var oAuthFlows = _oAuthFlowsFactory.CreateOAuthFlows(oAuthClientConfiguration.Name);
        var response   = await oAuthFlows.RunFlow(oAuthClientConfiguration, state, oAuthTesterViewModel.Username, oAuthTesterViewModel.Password, ResponseModes.FormPost);

        switch (response)
        {
        case OAuthRedirect oAuthRedirect:
            TempData[Common.State]        = oAuthRedirect.State;
            TempData[Common.CodeVerifier] = oAuthRedirect.CodeVerifier;
            TempData[TempDataNames.OAuthTesterConfigurationName] = oAuthTesterViewModel.ConfigurationName;
            return(Redirect(oAuthRedirect.Uri));

        case AccessTokenResponse accessTokenResponse:
            oAuthTesterViewModel.AccessTokenResponse = Utils.Mappers.OAuthMapper.Map(accessTokenResponse);
            break;

        case DeviceCodeResponse deviceCodeResponse:
            TempData[TempDataNames.OAuthTesterConfigurationName] = oAuthTesterViewModel.ConfigurationName;
            TempData.Write(TempDataNames.DeviceCodeResponse, deviceCodeResponse);
            return(RedirectToAction(nameof(PingDeviceToken)));

        case ErrorResponse errorResponse:
            return(ProcessOAuthClientErrorResponse(errorResponse));
        }

        return(View(oAuthTesterViewModel));
    }
Пример #3
0
    public IOAuthClientResponse CreateAuthorizationCodeRedirect(IEnumerable <string> scopes = null, string state = null)
    {
        var queryStringParams = new Dictionary <string, string>
        {
            { Common.ResponseType, ResponseTypes.Code },
            { Common.ClientId, _configuration.ClientId },
            { Common.RedirectUri, _configuration.RedirectUri },
            { Common.State, state ?? StateUtils.Generate() }
        }.AddScopes(scopes);

        var redirectUri = QueryHelpers.AddQueryString(_configuration.AuthorizeEndpoint, queryStringParams);

        var response = new OAuthRedirect
        {
            Uri   = redirectUri,
            State = state
        };

        return(response);
    }