Exemplo n.º 1
0
        public IEnumerator LoadArchivedAccessToken()
        {
            CachedString cache            = new CachedString("NocoAccessToken");
            string       tokenDescription = cache.GetString();

            if (tokenDescription != null)
            {
                AccessTokenDescriptor previousTokenDescriptor = JsonUtility.FromJson <AccessTokenDescriptor> (tokenDescription);
                if (cache.IsValid())
                {
                    if (debugAuthentification)
                    {
                        Debug.Log("Access token still valid for " + cache.RemainingValidity() + " seconds");
                    }
                    this.oauthAccessToken = previousTokenDescriptor;
                }
                else if (previousTokenDescriptor != null)
                {
                    if (debugAuthentification)
                    {
                        Debug.Log("Trying to use refresh token after access token lapsing " + tokenDescription);
                    }
                    NocoOAuthAccessTokenRequest request = new NocoOAuthAccessTokenRequest(this.clientId, this.clientSecret);
                    yield return(request.FetchAccessTokenFromRefreshToken(previousTokenDescriptor.refresh_token));

                    if (debugAuthentification)
                    {
                        Debug.Log("Refresh token call result:" + request.result);
                    }
                    this.oauthAccessToken = request.oauthAccessToken;
                    if (IsAuthenticated())
                    {
                        if (debugAuthentification)
                        {
                            Debug.Log("Refresh token succed: new access token found: " + this.oauthAccessToken);
                        }
                        this.oauthAccessToken.Save();
                    }
                }
            }
        }
Exemplo n.º 2
0
        public IEnumerator Authenticate(String username, String password)
        {
            if (username == null || username == "" || password == null || username == "")
            {
                Debug.Log("Missing credentials");
                yield return(null);
            }
            else
            {
                // Authentification
                if (debugAuthentification)
                {
                    Debug.Log("Noco Authentification...");
                }
                NocoOAuthAuthentificationRequest authentificationRequest = new NocoOAuthAuthentificationRequest();
                yield return(authentificationRequest.Launch(username, password, this.clientId));

                if (debugAuthentification)
                {
                    Debug.Log("Authentifiction call finished");
                }
                this.oauthCode = authentificationRequest.code;

                if (this.oauthCode != null)
                {
                    NocoOAuthAccessTokenRequest request = new NocoOAuthAccessTokenRequest(this.clientId, this.clientSecret);
                    yield return(request.FetchAccessTokenFromCode(this.oauthCode));

                    this.oauthAccessToken = request.oauthAccessToken;
                    if (IsAuthenticated())
                    {
                        this.oauthAccessToken.Save();
                    }
                }
                else
                {
                    Debug.Log("Authentifiction failed");
                }
            }
        }