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);
        }
예제 #2
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);
        }