Exemplo n.º 1
0
        public ValueTask <IdentityHttpResponse> Login(string state)
        {
            var requestDocument = new OAuth2LoginRequest(serviceProvider, redirectUrl, state);
            var requestBinding  = OAuth2Binding.GetBindingForDocument(requestDocument, BindingType.Form);
            var response        = requestBinding.GetResponse(loginUrl);

            return(new ValueTask <IdentityHttpResponse>(response));
        }
Exemplo n.º 2
0
        public ValueTask <IdentityHttpResponse> Logout(string state)
        {
            var requestDocument = new OAuth2LogoutRequest(
                serviceProvider: serviceProvider,
                redirectUrl: redirectUrlPostLogout,
                state: state
                );

            var requestBinding = OAuth2Binding.GetBindingForDocument(requestDocument, BindingType.Query);
            var response       = requestBinding.GetResponse(logoutUrl);

            return(new ValueTask <IdentityHttpResponse>(response));
        }
Exemplo n.º 3
0
        public ValueTask <LogoutModel> LogoutCallback(IdentityHttpRequest request)
        {
            var callbackBinding = OAuth2Binding.GetBindingForRequest(request, BindingDirection.Response);

            var callbackDocument = new OAuth2LogoutResponse(callbackBinding);

            var logout = new LogoutModel()
            {
                ServiceProvider = serviceProvider,
                OtherClaims     = callbackDocument.OtherClaims,
                State           = callbackDocument.State
            };

            return(new ValueTask <LogoutModel>(logout));
        }
Exemplo n.º 4
0
        public OAuth2LogoutResponse(Binding <JObject> binding)
        {
            if (binding.BindingDirection != this.BindingDirection)
            {
                throw new ArgumentException("Binding has the wrong binding direction for this document");
            }

            var json = binding.GetDocument();

            if (json == null)
            {
                return;
            }

            this.ServiceProvider = json[OAuth2Binding.ClientFormName]?.ToObject <string>();
            this.State           = json["state"]?.ToObject <string>();

            this.OtherClaims = OAuth2Binding.GetOtherClaims(json);
        }
Exemplo n.º 5
0
        public async ValueTask <IdentityModel> LoginCallback(IdentityHttpRequest request)
        {
            var callbackBinding = OAuth2Binding.GetBindingForRequest(request, BindingDirection.Response);

            var callbackDocument = new OAuth2LoginResponse(callbackBinding);

            var callbackServiceProvider = callbackDocument.ServiceProvider;
            var code = callbackDocument.AccessCode;

            if (code == null)
            {
                return(null);
            }

            if (serviceProvider != callbackServiceProvider)
            {
                throw new IdentityProviderException("Service Providers do not match", $"Received: {serviceProvider}, Expected: {callbackServiceProvider}");
            }

            //Get Token--------------------
            var requestTokenDocument = new OAuth2TokenRequest(serviceProvider, code);
            var requestTokenBinding  = OAuth2Binding.GetBindingForDocument(requestTokenDocument, BindingType.Query);

            var requestTokenAction = requestTokenBinding.GetResponse(tokenUrl);
            var requestToken       = WebRequest.Create(requestTokenAction.RedirectUrl);
            var responseToken      = await requestToken.GetResponseAsync();

            var responseTokenBinding  = OAuth2Binding.GetBindingForResponse(responseToken, BindingDirection.Response);
            var responseTokenDocument = new OAuth2TokenResponse(responseTokenBinding);

            //Get Identity---------------
            var requestIdentityDocument = new OAuth2IdentityRequest(serviceProvider, responseTokenDocument.Token);
            var requestIdentityBinding  = OAuth2Binding.GetBindingForDocument(requestIdentityDocument, BindingType.Query);

            var requestIdentityAction = requestIdentityBinding.GetResponse(identityUrl);
            var requestIdentity       = WebRequest.Create(requestIdentityAction.RedirectUrl);
            var responseIdentity      = await requestIdentity.GetResponseAsync();

            var responseIdentityBinding  = OAuth2Binding.GetBindingForResponse(responseIdentity, BindingDirection.Response);
            var responseIdentityDocument = new OAuth2IdentityResponse(responseIdentityBinding);

            if (responseIdentityDocument.ServiceProvider != serviceProvider)
            {
                return(null);
            }

            if (String.IsNullOrWhiteSpace(responseIdentityDocument.UserID))
            {
                return(null);
            }

            var identity = new IdentityModel()
            {
                UserID          = responseIdentityDocument.UserID,
                UserName        = responseIdentityDocument.UserName,
                Name            = responseIdentityDocument.UserName,
                ServiceProvider = responseIdentityDocument.ServiceProvider,
                Roles           = responseIdentityDocument.Roles,
                OtherClaims     = null,
                State           = null,
                AccessToken     = responseTokenDocument.Token
            };

            return(identity);
        }