Example #1
0
        public IAsyncOperation InitOAuth(AuthorizationInfo authorizationInfo)
        {
            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.RefreshTokenAsync(tokenUrl, authorizationInfo.ClientId, authorizationInfo.RefreshToken)
                .Then(authInfo =>
                {
                    AuthorizationInfo = authInfo;
                    done.SetCompleted();
                })
                .Catch(ex =>
                {
                    done.SetException(ex);
                });
            }
            catch (ApiException ex)
            {
                done.SetException(new ApiException(ex.ErrorCode, "Failed to renew AuthorizationInfo: " + ex.Message));
            }

            return(done);
        }
Example #2
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);
        }