Example #1
0
        public IAsyncOperation InitOAuth(string clientId, string clientSecret = null,
                                         string realm = null, string appName = null, string scopeSeparator = ":", bool usePKCE = false,
                                         Dictionary <string, string> aditionalQueryStringParams = null, bool scope_offline = false)
        {
            String[] scopes = null;
            if (scope_offline)
            {
                scopes = new string[] { "offline_access" };
            }
            else
            {
                scopes = new string[] { };
            }


            var tokenUrl = TokenPath ?? "https://sso.simva.e-ucm.es/auth/realms/simva/protocol/openid-connect/token";
            var authUrl  = AuthPath ?? "https://sso.simva.e-ucm.es/auth/realms/simva/protocol/openid-connect/auth";

            var done = new AsyncCompletionSource();

            OpenIdUtility.LoginWithAccessCode(authUrl, tokenUrl, clientId, null, string.Join(scopeSeparator, scopes), usePKCE)
            .Then(authInfo =>
            {
                AuthorizationInfo = authInfo;
                done.SetCompleted();
            })
            .Catch(error =>
            {
                done.SetException(new ApiException(500, error.Message));
            });

            return(done);
        }
Example #2
0
        public IAsyncOperation ContinueOAuth(string clientId)
        {
            var scopes = new string[] { };

            var tokenUrl = TokenPath ?? "https://sso.simva.e-ucm.es/auth/realms/simva/protocol/openid-connect/token";
            var authUrl  = AuthPath ?? "https://sso.simva.e-ucm.es/auth/realms/simva/protocol/openid-connect/auth";

            var done = new AsyncCompletionSource();

            try
            {
                OpenIdUtility.TryContinueLogin(tokenUrl, clientId)
                .Then(authInfo =>
                {
                    AuthorizationInfo = authInfo;
                    done.SetCompleted();
                });
            }
            catch (ApiException ex)
            {
                done.SetException(new ApiException(ex.ErrorCode, "Failed to renew AuthorizationInfo: " + ex.Message));
            }

            return(done);
        }
Example #3
0
        /// <summary>
        /// Update parameters based on authentication.
        /// </summary>
        /// <param name="queryParams">Query parameters.</param>
        /// <param name="headerParams">Header parameters.</param>
        /// <param name="authSettings">Authentication settings.</param>
        public IAsyncOperation UpdateParamsForAuth(Dictionary <String, String> queryParams, Dictionary <String, String> headerParams, string[] authSettings, bool async)
        {
            var result = new AsyncCompletionSource();

            if (authSettings == null || authSettings.Length == 0)
            {
                result.SetCompleted();
                return(result);
            }

            foreach (string auth in authSettings)
            {
                // determine which one to use
                switch (auth)
                {
                case "OAuth2":

                    if (AuthorizationInfo == null)
                    {
                        result.SetException(new ApiException(500, "OAuth not inited, please init the authorization in ApiClient with InitOauth or set up the AuthorizationInfo!"));
                        return(result);
                    }
                    var    tokenUrl           = TokenPath ?? "https://sso.simva.e-ucm.es/auth/realms/simva/protocol/openid-connect/token";
                    Action addAuthAndComplete = () =>
                    {
                        var tokenType = AuthorizationInfo.TokenType.First().ToString().ToUpper() + AuthorizationInfo.TokenType.Substring(1);
                        headerParams.Add("Authorization", tokenType + " " + AuthorizationInfo.AccessToken);
                        result.SetCompleted();
                    };
                    if (AuthorizationInfo.Expired)
                    {
                        if (async || Application.platform == RuntimePlatform.WebGLPlayer)
                        {
                            OpenIdUtility.RefreshTokenAsync(tokenUrl, AuthorizationInfo.ClientId, AuthorizationInfo.RefreshToken)
                            .Then(authInfo =>
                            {
                                AuthorizationInfo = authInfo;
                                addAuthAndComplete();
                            })
                            .Catch(ex =>
                            {
                                result.SetException(ex);
                            });
                        }
                        else
                        {
                            AuthorizationInfo = OpenIdUtility.RefreshToken(tokenUrl, AuthorizationInfo.ClientId, AuthorizationInfo.RefreshToken);
                            addAuthAndComplete();
                        }
                    }
                    else
                    {
                        addAuthAndComplete();
                    }

                    break;

                default:
                    //TODO show warning about security definition not found
                    result.SetCompleted();
                    break;
                }
            }

            return(result);
        }