Ejemplo n.º 1
0
        private async Task <TokenResponse> GetRefreshToken(IntPtr requestHandle)
        {
            int    ret   = (int)OAuth2Error.None;
            IntPtr error = IntPtr.Zero;
            IntPtr id    = IntPtr.Zero;

            lock (_taskResponseMap)
            {
                id = (IntPtr)_responseCompletionId++;
            }
            TaskCompletionSource <TokenResponse> tcsaccessTokenResponse = new TaskCompletionSource <TokenResponse>();

            _taskResponseMap[id] = tcsaccessTokenResponse;
            ret = Interop.Manager.RefreshAccessToken(_managerHandle, requestHandle, _accessTokenCb, id);
            if (ret != (int)OAuth2Error.None || error != IntPtr.Zero)
            {
                if (error != IntPtr.Zero)
                {
                    throw ErrorFactory.GetException(error);
                }
                else
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }
            await tcsaccessTokenResponse.Task.ConfigureAwait(false);

            Interop.Request.Destroy(requestHandle);
            return(tcsaccessTokenResponse.Task.Result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructor for Authoirzer instances
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public Authorizer()
        {
            int ret = Interop.Manager.Create(out _managerHandle);

            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Clear the cache
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public void ClearCache()
        {
            int ret = (int)OAuth2Error.None;

            ret = Interop.Manager.ClearCache(_managerHandle);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }
        }
Ejemplo n.º 4
0
        public string GetCustomValue(string key)
        {
            IntPtr value;
            int    ret = Interop.Response.GetCustomData(_responseHandle, key, out value);

            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }
            return(Marshal.PtrToStringAnsi(value));
        }
Ejemplo n.º 5
0
        internal TokenResponse GetAccessTokenByCode(IntPtr requestHandle)
        {
            int           ret      = (int)OAuth2Error.None;
            IntPtr        error    = IntPtr.Zero;
            TokenResponse response = null;

            Interop.Manager.Oauth2AccessTokenCallback accessTokenCb = (IntPtr responseHandle, IntPtr usrData) =>
            {
                if (responseHandle == IntPtr.Zero)
                {
                    Log.Error(ErrorFactory.LogTag, "Error occured");
                    throw (new ArgumentNullException());
                }

                Interop.Response.GetError(responseHandle, out error);
                if (error != IntPtr.Zero)
                {
                    Log.Error(ErrorFactory.LogTag, "Server Error occured");
                }
                else
                {
                    IntPtr accessToken = IntPtr.Zero;
                    ret = Interop.Response.GetAccessToken(responseHandle, out accessToken);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Failed to get access token");
                        throw ErrorFactory.GetException(ret);
                    }

                    IntPtr tokenType;
                    ret = Interop.Response.GetTokenType(responseHandle, out tokenType);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Debug(ErrorFactory.LogTag, "TokenType can't be found");
                    }

                    long expiresIn = -1;
                    ret = Interop.Response.GetExpiresIn(responseHandle, out expiresIn);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Debug(ErrorFactory.LogTag, "ExpiresIn can't be found");
                    }

                    IntPtr refreshToken;
                    ret = Interop.Response.GetRefreshToken(responseHandle, out refreshToken);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Debug(ErrorFactory.LogTag, "Refresh Token can't be found");
                    }

                    IntPtr scope;
                    ret = Interop.Response.GetScope(responseHandle, out scope);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Debug(ErrorFactory.LogTag, "Scope can't be found");
                    }

                    IntPtr state;
                    ret = Interop.Response.GetState(responseHandle, out state);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Debug(ErrorFactory.LogTag, "State can't be found");
                    }

                    IEnumerable <string> scopes = (scope == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(scope)?.Split(' ');

                    var token = new AccessToken();
                    token.Token     = (accessToken == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(accessToken);
                    token.TokenType = (tokenType == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(tokenType);
                    token.Scope     = scopes;
                    token.ExpiresIn = expiresIn;

                    response              = new TokenResponse(responseHandle);
                    response.AccessToken  = token;
                    response.State        = (state == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(state);
                    response.RefreshToken = (refreshToken == IntPtr.Zero) ? null : new RefreshToken()
                    {
                        Token = Marshal.PtrToStringAnsi(refreshToken)
                    };
                }
            };

            ret = Interop.Manager.RequestAccessToken(_managerHandle, requestHandle, accessTokenCb, IntPtr.Zero);
            Interop.Request.Destroy(requestHandle);
            if (ret != (int)OAuth2Error.None || error != IntPtr.Zero)
            {
                if (error != IntPtr.Zero)
                {
                    throw ErrorFactory.GetException(error);
                }
                else
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed : " + ret);
                    throw ErrorFactory.GetException(ret);
                }
            }

            return(response);
        }
Ejemplo n.º 6
0
        // Fill device request handle for refreshing access token
        internal IntPtr GetRequestHandle(RefreshTokenRequest request)
        {
            IntPtr requestHandle;
            int    ret = Interop.Request.Create(out requestHandle);

            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetRefreshTokenUrl(requestHandle, request.TokenEndpoint.ToString());
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetGrantType(requestHandle, Interop.GrantType.Refresh);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetRefreshToken(requestHandle, request.RefreshToken);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            if (request.ClientSecrets.Id != null)
            {
                ret = Interop.Request.SetClientId(requestHandle, request.ClientSecrets.Id);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.ClientSecrets.Secret != null)
            {
                ret = Interop.Request.SetClientSecret(requestHandle, request.ClientSecrets.Secret);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.Scopes != null)
            {
                string scope = string.Join(" ", request.Scopes);
                ret = Interop.Request.SetScope(requestHandle, scope);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            ret = Interop.Request.SetClientAuthenticationType(requestHandle, (int)request.AuthenticationScheme);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            return(requestHandle);
        }
Ejemplo n.º 7
0
        private static void AccessTokenCb(IntPtr responseHandle, IntPtr usrData)
        {
            int           ret                  = (int)OAuth2Error.None;
            IntPtr        error                = IntPtr.Zero;
            TokenResponse response             = null;
            IntPtr        responseCompletionId = usrData;
            TaskCompletionSource <TokenResponse> responseCompletionSource = _taskResponseMap[responseCompletionId];

            _taskResponseMap.Remove(responseCompletionId);

            Interop.Response.GetError(responseHandle, out error);
            if (error != IntPtr.Zero)
            {
                Log.Error(ErrorFactory.LogTag, "Error occured");
                responseCompletionSource.SetException(ErrorFactory.GetException(error));
            }
            else
            {
                IntPtr accessToken;
                ret = Interop.Response.GetAccessToken(responseHandle, out accessToken);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    responseCompletionSource.SetException(ErrorFactory.GetException(ret));
                    return;
                }

                IntPtr tokenType;
                ret = Interop.Response.GetTokenType(responseHandle, out tokenType);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Failed to get token type");
                }

                long expiresIn;
                ret = Interop.Response.GetExpiresIn(responseHandle, out expiresIn);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Failed to get expires in");
                }

                IntPtr refreshToken;
                ret = Interop.Response.GetRefreshToken(responseHandle, out refreshToken);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    responseCompletionSource.SetException(ErrorFactory.GetException(ret));
                    return;
                }

                IntPtr scope;
                ret = Interop.Response.GetScope(responseHandle, out scope);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Failed to get scope");
                }

                IEnumerable <string> scopes = (scope == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(scope)?.Split(' ');

                var token = new AccessToken();
                token.Token     = (accessToken == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(accessToken);
                token.TokenType = (tokenType == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(tokenType);
                token.Scope     = scopes;
                token.ExpiresIn = expiresIn;

                response              = new TokenResponse(responseHandle);
                response.AccessToken  = token;
                response.RefreshToken = (refreshToken == IntPtr.Zero) ? null : new RefreshToken()
                {
                    Token = Marshal.PtrToStringAnsi(refreshToken)
                };
                responseCompletionSource.SetResult(response);
            }
        }
Ejemplo n.º 8
0
        // Fill device request handle for Authorization code grant
        private IntPtr GetRequestHandle(CodeGrantAuthorizationRequest request)
        {
            if (request == null)
            {
                Log.Error(ErrorFactory.LogTag, "Invalid request or request is null");
                throw ErrorFactory.GetException((int)OAuth2Error.InvalidParameter);
            }

            IntPtr requestHandle;
            int    ret = Interop.Request.Create(out requestHandle);

            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetAuthEndPointUrl(requestHandle, request.AuthorizationEndpoint.ToString());
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetResponseType(requestHandle, Interop.ResponseType.Code);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            if (request.ClientSecrets.Id != null)
            {
                ret = Interop.Request.SetClientId(requestHandle, request.ClientSecrets.Id);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.ClientSecrets.Secret != null)
            {
                ret = Interop.Request.SetClientSecret(requestHandle, request.ClientSecrets.Secret);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.RedirectionEndPoint != null)
            {
                ret = Interop.Request.SetRedirectionUrl(requestHandle, request.RedirectionEndPoint.OriginalString);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.Scopes != null)
            {
                string scope = string.Join(" ", request.Scopes);
                ret = Interop.Request.SetScope(requestHandle, scope);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.State != null)
            {
                ret = Interop.Request.SetState(requestHandle, request.State);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.CustomData != null)
            {
                foreach (var item in request.CustomData)
                {
                    ret = Interop.Request.AddCustomData(requestHandle, item.Key, item.Value);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }
                }
            }

            return(requestHandle);
        }
Ejemplo n.º 9
0
        // Fill device request handle for access token
        private IntPtr GetRequestHandle(CodeGrantTokenRequest request)
        {
            if (request == null)
            {
                Log.Error(ErrorFactory.LogTag, "Invalid request or request is null");
                throw ErrorFactory.GetException((int)OAuth2Error.InvalidParameter);
            }

            IntPtr requestHandle;
            int    ret = Interop.Request.Create(out requestHandle);

            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetGrantType(requestHandle, Interop.GrantType.AuthCode);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetAuthorizationCode(requestHandle, request.Code);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetTokenEndPointUrl(requestHandle, request.TokenEndpoint.ToString());
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetRedirectionUrl(requestHandle, request.RedirectionEndPoint.ToString());
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetClientId(requestHandle, request.ClientSecrets.Id);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            if (request.ClientSecrets.Secret != null)
            {
                ret = Interop.Request.SetClientSecret(requestHandle, request.ClientSecrets.Secret);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.CustomData != null)
            {
                foreach (var item in request.CustomData)
                {
                    ret = Interop.Request.AddCustomData(requestHandle, item.Key, item.Value);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }
                }
            }

            ret = Interop.Request.SetClientAuthenticationType(requestHandle, (int)request.AuthenticationScheme);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            return(requestHandle);
        }
Ejemplo n.º 10
0
        private TokenResponse GetAuthorizationResponse(IntPtr requestHandle)
        {
            IntPtr        error    = IntPtr.Zero;
            TokenResponse response = null;
            int           ret      = (int)OAuth2Error.None;

            Interop.Manager.Oauth2AuthGrantCallback authGrantCb = (IntPtr responseHandle, IntPtr usrData) =>
            {
                if (responseHandle == IntPtr.Zero)
                {
                    Log.Error(ErrorFactory.LogTag, "Error occured");
                    throw (new ArgumentNullException());
                }

                Interop.Response.GetError(responseHandle, out error);
                if (error != IntPtr.Zero)
                {
                    Log.Error(ErrorFactory.LogTag, "Server Error occured");
                }
                else
                {
                    IntPtr accessToken;
                    ret = Interop.Response.GetAccessToken(responseHandle, out accessToken);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }

                    IntPtr tokenType;
                    ret = Interop.Response.GetTokenType(responseHandle, out tokenType);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }

                    long expiresIn;
                    ret = Interop.Response.GetExpiresIn(responseHandle, out expiresIn);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }

                    IntPtr scope;
                    ret = Interop.Response.GetScope(responseHandle, out scope);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }

                    IntPtr state;
                    ret = Interop.Response.GetState(responseHandle, out state);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }

                    IEnumerable <string> scopes = (scope == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(scope)?.Split(' ');

                    var token = new AccessToken()
                    {
                        Token = Marshal.PtrToStringAnsi(accessToken), ExpiresIn = expiresIn, Scope = scopes, TokenType = Marshal.PtrToStringAnsi(tokenType)
                    };
                    response = new TokenResponse(responseHandle)
                    {
                        AccessToken = token, State = Marshal.PtrToStringAnsi(state), RefreshToken = null
                    };
                }
            };

            ret = Interop.Manager.RequestAuthorizationGrant(_managerHandle, requestHandle, authGrantCb, IntPtr.Zero);
            Interop.Request.Destroy(requestHandle);
            if (ret != (int)OAuth2Error.None || error != IntPtr.Zero)
            {
                if (error != IntPtr.Zero)
                {
                    throw ErrorFactory.GetException(error);
                }
                else
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            return(response);
        }
Ejemplo n.º 11
0
        // Fill device request handle for access token
        private IntPtr GetRequestHandle(ResourceOwnerPwdCredentialsTokenRequest request)
        {
            if (request == null)
            {
                Log.Error(ErrorFactory.LogTag, "Invalid request or request is null");
                throw ErrorFactory.GetException((int)OAuth2Error.InvalidParameter);
            }

            IntPtr requestHandle;
            int    ret = Interop.Request.Create(out requestHandle);

            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetTokenEndPointUrl(requestHandle, request.TokenEndpoint.ToString());
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetRedirectionUrl(requestHandle, request.RedirectionEndPoint.ToString());
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetGrantType(requestHandle, Interop.GrantType.Password);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetUserName(requestHandle, request.Username);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            ret = Interop.Request.SetPassword(requestHandle, request.Password);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            if (request.ClientSecrets.Id != null)
            {
                ret = Interop.Request.SetClientId(requestHandle, request.ClientSecrets.Id);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.ClientSecrets.Secret != null)
            {
                ret = Interop.Request.SetClientSecret(requestHandle, request.ClientSecrets.Secret);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.Scopes != null)
            {
                string scope = string.Join(" ", request.Scopes);
                ret = Interop.Request.SetScope(requestHandle, scope);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            if (request.CustomData != null)
            {
                foreach (var item in request.CustomData)
                {
                    ret = Interop.Request.AddCustomData(requestHandle, item.Key, item.Value);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }
                }
            }

            if (request.State != null)
            {
                ret = Interop.Request.SetState(requestHandle, request.State);
                if (ret != (int)OAuth2Error.None)
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            ret = Interop.Request.SetClientAuthenticationType(requestHandle, (int)request.AuthenticationScheme);
            if (ret != (int)OAuth2Error.None)
            {
                Log.Error(ErrorFactory.LogTag, "Interop failed");
                throw ErrorFactory.GetException(ret);
            }

            return(requestHandle);
        }
Ejemplo n.º 12
0
        private AuthorizationResponse GetAuthorizationResponse(IntPtr requestHandle)
        {
            AuthorizationResponse response = null;
            int    ret   = (int)OAuth2Error.None;
            IntPtr error = IntPtr.Zero;

            Interop.Manager.Oauth2AuthGrantCallback authGrantCb = (IntPtr responseHandle, IntPtr usrData) =>
            {
                if (responseHandle == IntPtr.Zero)
                {
                    Log.Error(ErrorFactory.LogTag, "Error occured");
                    throw (new ArgumentNullException());
                }

                Interop.Response.GetError(responseHandle, out error);
                if (error == IntPtr.Zero)
                {
                    Log.Warn(ErrorFactory.LogTag, "Error occured");
                    throw ErrorFactory.GetException(error);
                }
                else
                {
                    IntPtr authorizationCode;
                    ret = Interop.Response.GetAuthorizationCode(responseHandle, out authorizationCode);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }

                    IntPtr state;
                    ret = Interop.Response.GetState(responseHandle, out state);
                    if (ret != (int)OAuth2Error.None)
                    {
                        Log.Error(ErrorFactory.LogTag, "Interop failed");
                        throw ErrorFactory.GetException(ret);
                    }

                    response = new AuthorizationResponse(responseHandle)
                    {
                        Code = Marshal.PtrToStringAnsi(authorizationCode), State = Marshal.PtrToStringAnsi(state)
                    };
                }
            };

            ret = Interop.Manager.RequestAuthorizationGrant(_managerHandle, requestHandle, authGrantCb, IntPtr.Zero);
            Interop.Request.Destroy(requestHandle);
            if (ret != (int)OAuth2Error.None || error != IntPtr.Zero)
            {
                if (error != IntPtr.Zero)
                {
                    throw ErrorFactory.GetException(error);
                }
                else
                {
                    Log.Error(ErrorFactory.LogTag, "Interop failed");
                    throw ErrorFactory.GetException(ret);
                }
            }

            return(response);
        }