Exemplo n.º 1
0
        public string f_get_userInfoOrCreateNewIfNotExist(OPEN_AUTH_CLIENT clientCredentials)
        {
            if ((this._authenticator == null || this._driveService == null) && (clientCredentials == null || string.IsNullOrEmpty(clientCredentials.CODE_AUTH_CLIENT)))
            {
                return(JsonConvert.SerializeObject(new { Ok = false, tokenAccess = this._token, Message = "CODE_AUTH_CLIENT must be not null" }));
            }

            IAuthenticator auth    = null;
            DriveService   service = null;

            if (this._authenticator == null || this._driveService == null)
            {
                try
                {
                    auth    = GetCredentials(clientCredentials);
                    service = BuildService(auth);
                }
                catch (Exception e) { return(JsonConvert.SerializeObject(new { Ok = false, tokenAccess = this._token, Message = e.Message })); }
            }
            else
            {
                return(f_get_userInfo());
            }

            if (auth == null || service == null)
            {
                return(JsonConvert.SerializeObject(new { Ok = false, tokenAccess = this._token, Message = "Redirect user to authentication" }));
            }
            else
            {
                this._authenticator = auth;
                this._driveService  = service;
                return(this.f_get_userInfo());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Retrieve an IAuthenticator instance using the provided state.
        /// </summary>
        /// <param name="credentials">OAuth 2.0 credentials to use.</param>
        /// <returns>Authenticator using the provided OAuth 2.0 credentials</returns>
        IAuthenticator GetAuthenticatorFromState(IAuthorizationState credentials, OPEN_AUTH_CLIENT clientCredentials)
        {
            var provider = new StoredStateClient(GoogleAuthenticationServer.Description, clientCredentials.CLIENT_ID, clientCredentials.CLIENT_SECRET, credentials);
            var auth     = new OAuth2Authenticator <StoredStateClient>(provider, StoredStateClient.GetState);

            auth.LoadAccessToken();
            return(auth);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Retrieve credentials using the provided authorization code.
        ///
        /// This function exchanges the authorization code for an access token and
        /// queries the UserInfo API to retrieve the user's e-mail address. If a
        /// refresh token has been retrieved along with an access token, it is stored
        /// in the application database using the user's e-mail address as key. If no
        /// refresh token has been retrieved, the function checks in the application
        /// database for one and returns it if found or throws a NoRefreshTokenException
        /// with the authorization URL to redirect the user to.
        /// </summary>
        /// <param name="authorizationCode">Authorization code to use to retrieve an access token.</param>
        /// <param name="state">State to set to the authorization URL in case of error.</param>
        /// <returns>OAuth 2.0 credentials instance containing an access and refresh token.</returns>
        /// <exception cref="CodeExchangeException">
        /// An error occurred while exchanging the authorization code.
        /// </exception>
        /// <exception cref="NoRefreshTokenException">
        /// No refresh token could be retrieved from the available sources.
        /// </exception>
        IAuthenticator GetCredentials(OPEN_AUTH_CLIENT clientCredentials)
        {
            String emailAddress = "";

            try
            {
                IAuthorizationState credentials = ExchangeCode(clientCredentials);
                Userinfo            userInfo    = GetUserInfo(credentials, clientCredentials);
                String userId = userInfo.Id;
                emailAddress = userInfo.Email;
                ////if (!String.IsNullOrEmpty(credentials.RefreshToken))
                ////{
                ////    StoreCredentials(userId, credentials);
                ////    return GetAuthenticatorFromState(credentials, clientCredentials);
                ////}
                ////else
                ////{
                ////    credentials = GetStoredCredentials(userId);
                ////    if (credentials != null && !String.IsNullOrEmpty(credentials.RefreshToken))
                ////    {
                ////        return GetAuthenticatorFromState(credentials, clientCredentials);
                ////    }
                ////}

                IAuthenticator au = GetAuthenticatorFromState(credentials, clientCredentials);
                return(au);
            }
            catch { }

            //catch (CodeExchangeException e)
            //{
            //    Console.WriteLine("An error occurred during code exchange. " + e.Message);
            //    ////// Drive apps should try to retrieve the user and credentials for the current
            //    ////// session.
            //    ////// If none is available, redirect the user to the authorization URL.
            //    ////e.AuthorizationUrl = GetAuthorizationUrl(emailAddress, state);
            //    ////throw e;
            //}
            //catch (NoUserIdException eu)
            //{
            //    Console.WriteLine("No user ID could be retrieved. " + eu.Message);
            //}
            ////////// No refresh token has been retrieved.
            ////////String authorizationUrl = GetAuthorizationUrl(emailAddress, state);
            ////////throw new NoRefreshTokenException(authorizationUrl);

            return(null);
        }
Exemplo n.º 4
0
        IAuthorizationState ExchangeCode(OPEN_AUTH_CLIENT clientCredentials)
        {
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, clientCredentials.CLIENT_ID, clientCredentials.CLIENT_SECRET);
            IAuthorizationState state = new AuthorizationState();

            state.Callback = new Uri(clientCredentials.REDIRECT_URI);
            try
            {
                state       = provider.ProcessUserAuthorization(clientCredentials.CODE_AUTH_CLIENT, state);
                this._token = state.AccessToken;
                return(state);
            }
            catch (ProtocolException)
            {
                throw new CodeExchangeException(null);
            }
        }
Exemplo n.º 5
0
        public string f_create_TokenNew(OPEN_AUTH_CLIENT clientCredentials)
        {
            IAuthenticator auth    = null;
            DriveService   service = null;

            try {
                auth    = GetCredentials(clientCredentials);
                service = BuildService(auth);
            }
            catch (Exception e) { return(JsonConvert.SerializeObject(new { Ok = false, tokenAccess = this._token, Message = e.Message })); }

            if (auth == null || service == null)
            {
                return(JsonConvert.SerializeObject(new { Ok = false, tokenAccess = this._token, Message = "redirect user to authentication" }));
            }
            else
            {
                this._authenticator = auth;
                this._driveService  = service;
                return(this.f_get_userInfo());
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Send a request to the UserInfo API to retrieve the user's information.
 /// </summary>
 /// <param name="credentials">OAuth 2.0 credentials to authorize the request.</param>
 /// <returns>User's information.</returns>
 /// <exception cref="NoUserIdException">An error occurred.</exception>
 Userinfo GetUserInfo(IAuthorizationState credentials, OPEN_AUTH_CLIENT clientCredentials)
 {
     return(GetUserInfo(GetAuthenticatorFromState(credentials, clientCredentials)));
 }