Beispiel #1
0
        internal static async Task <OAuth2Credentials> AuthorizeAsync(OAuth2App app, OAuth2EndPoint endPoint)
        {
            var credentials = LoadCredentials(app);

            if (credentials?.AccessToken == null)
            {
                var server = StartRedirectServer(app.RedirectUri).ConfigureAwait(false);

                var authUri = $"{endPoint.BaseUri.TrimEnd('/')}/{endPoint.AuthResource.TrimStart('/')}?response_type=code&client_id={app.ClientId}";
                Process.Start(authUri);

                var code = await server;

                credentials = await GetCredentialsAsync(code, app, endPoint).ConfigureAwait(false);

                SaveCredentials(credentials, app);
            }
            else if (credentials.Expires < DateTime.Now)
            {
                credentials = await UpdateCredentialsAsync(credentials, app, endPoint).ConfigureAwait(false);
            }

            return(credentials);
        }
Beispiel #2
0
 private protected async Task AuthorizeAsync(OAuth2App app, OAuth2EndPoint endPoint)
 {
     _app         = app;
     _endPoint    = endPoint;
     _credentials = await OAuth2Broker.AuthorizeAsync(app, endPoint).ConfigureAwait(false);
 }
Beispiel #3
0
        internal static async Task <OAuth2Credentials> UpdateCredentialsAsync(OAuth2Credentials credentials, OAuth2App app, OAuth2EndPoint endPoint)
        {
            if (credentials?.AccessToken != null && DateTime.Now < credentials.Expires)
            {
                return(credentials);
            }

            var client = new RestClient(endPoint.BaseUri.TrimEnd('/') + '/');

            var tokenRequest = new RestRequest(endPoint.QueryTokenResource, Method.POST, DataFormat.FormUrlEncoded);

            tokenRequest.AddParameter("grant_type", "refresh_token");
            tokenRequest.AddParameter("client_id", app.ClientId);
            tokenRequest.AddParameter("client_secret", app.ClientSecret);
            tokenRequest.AddParameter("refresh_token", credentials.RefreshToken);

            var(response, _) = await client.SendAsync(tokenRequest).ConfigureAwait(false);

            var result = await response.DeserializeContentAsync <_QueryTokenResult>().ConfigureAwait(false);

            credentials = new OAuth2Credentials()
            {
                AccessToken  = result.access_token,
                RefreshToken = result.refresh_token,
                Expires      = DateTime.Now + TimeSpan.FromSeconds(result.expires_in),
            };
            SaveCredentials(credentials, app);

            return(credentials);
        }
Beispiel #4
0
        private static async Task <OAuth2Credentials> GetCredentialsAsync(string code, OAuth2App app, OAuth2EndPoint endPoint)
        {
            var client = new RestClient(endPoint.BaseUri);

            var tokenRequest = new RestRequest(endPoint.QueryTokenResource, Method.POST, DataFormat.FormUrlEncoded);

            tokenRequest.AddParameter("grant_type", "authorization_code");
            tokenRequest.AddParameter("code", code);
            tokenRequest.AddParameter("client_id", app.ClientId);
            tokenRequest.AddParameter("client_secret", app.ClientSecret);

            var(response, _) = await client.SendAsync(tokenRequest).ConfigureAwait(false);

            var result = await response.DeserializeContentAsync <_QueryTokenResult>().ConfigureAwait(false);

            return(new OAuth2Credentials()
            {
                AccessToken = result.access_token,
                RefreshToken = result.refresh_token,
                Expires = DateTime.Now + TimeSpan.FromSeconds(result.expires_in),
            });
        }