// Fill device request handle for Authorization code grant private IntPtr GetRequestHandle(ImplicitGrantAuthorizationRequest 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.Token); 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.RedirectionEndPoint != null) { ret = Interop.Request.SetRedirectionUrl(requestHandle, request.RedirectionEndPoint.ToString()); 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); }
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); }
// 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); }
// 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); }
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); }
// 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); }