Exemplo n.º 1
0
        public async Task <IActionResult> Token([FromBody] ResourceOwnerPasswordCredentialsGrantRequestModel model)
        {
            if ("password".Equals(model.GrantType, StringComparison.OrdinalIgnoreCase))
            {
                var signinCommand = new PasswordAuthenticateCommand
                {
                    UserName = model.UserName,
                    Password = model.Password
                };
                var authResult = await _sagaBus.InvokeAsync <PasswordAuthenticateCommand, AuthenticationResult>(signinCommand);

                if (!authResult.IsCredentialVaild)
                {
                    return(Unauthorized());
                }
                var authorizeCommand = new ResourceOwnerPasswordCredentialsGrantCommand
                {
                    ClientId     = model.ClientId,
                    UserName     = authResult.User.UserName,
                    ClientSecret = model.ClientSecret,
                    ScopeNames   = model.Scope.Split(' ')
                };
                var oauthResult = await _sagaBus.InvokeAsync <ResourceOwnerPasswordCredentialsGrantCommand, OAuth20Result>(authorizeCommand);

                switch (oauthResult.State)
                {
                case OAuth20State.Finished:
                    return(Ok(TokenResponseModel.FromOAuth20Result(oauthResult)));

                default:
                    return(BadRequest());
                }
            }
            return(BadRequest());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Token([FromBody] AuthorizationCodeGrantRequestModel model)
        {
            if ("authorization_code".Equals(model.GrantType, StringComparison.OrdinalIgnoreCase))
            {
                var message = new AccessTokenRequestMessage(model.Code)
                {
                    ClientId     = model.ClientId,
                    ClientSecret = model.ClientSecret,
                    Code         = model.Code,
                    RedirectUri  = model.ClientSecret
                };
                var result = await _sagaBus.SendAsync <AccessTokenRequestMessage, OAuth20Result>(message);

                switch (result.State)
                {
                case OAuth20State.AuthorizationCodeGenerated:
                    return(BadRequest());

                case OAuth20State.Finished:
                    return(Ok(TokenResponseModel.FromOAuth20Result(result)));
                }
            }
            return(BadRequest());
        }