Exemple #1
0
        public static async Task <string> GetExactOnlineAccessToken(this IBotContext context)
        {
            AuthenticationSettings authenticationSettings = AuthenticationSettings.GetFromAppSettings();
            AuthenticationResult   authenticationResult;

            string authResultKey = AuthenticationConstants.AuthDialogId_ExactOnline + '_' + AuthenticationConstants.AuthResultKey;

            if (context.UserData.TryGetValue(authResultKey, out authenticationResult))
            {
                try
                {
                    var tokenCache = TokenCacheFactory.SetTokenCache(authenticationResult.TokenCache);

                    var result = await ExactOnlineHelper.GetToken(authenticationResult.UserUniqueId);

                    authenticationResult.AccessToken       = result.AccessToken;
                    authenticationResult.ExpiresOnUtcTicks = result.ExpiresOnUtcTicks;
                    authenticationResult.TokenCache        = tokenCache.Serialize();
                    context.StoreAuthResult(authenticationResult);
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Failed to renew token: " + ex.Message);
                    await context.PostAsync("Your credentials expired and could not be renewed automatically!");

                    await context.Logout(authenticationSettings);

                    return(null);
                }
                return(authenticationResult.AccessToken);
            }

            return(null);
        }
Exemple #2
0
        public static async Task <AuthenticationResult> GetToken(string uniqueUserId = "")
        {
            // TODO: need to ensure that the unique user id part is included in the token cache
            var        tokenCache = TokenCacheFactory.GetTokenCache();
            OAuthToken token      = tokenCache.GetToken();

            // if there's a token but it's expired; we need to use the refresh token to get a new one
            if (token?.Expired() == true)
            {
                ExactOnlineClient client = GetClient();
                token.AccessToken       = client.GetCurrentToken(token.RefreshToken);
                token.ExpiresOnUtcTicks = client.ExpiresAt.Ticks;
                tokenCache.CacheToken(token);
            }

            AuthenticationResult authResult = ConvertAuthenticationResult(token, tokenCache);

            return(await Task.FromResult(authResult));
        }
Exemple #3
0
        public static ExactOnlineConnector GetConnector()
        {
            var tokenCache = TokenCacheFactory.GetTokenCache();

            if (tokenCache == null)
            {
                throw new ArgumentException("Cannot create Connector instance when the tokencache is null.");
            }

            OAuthToken token = tokenCache.GetToken();

            if (token == null || String.IsNullOrEmpty(token.UserUniqueId))
            {
                throw new ArgumentException("Cannot create Connector instance when there's no cached token or the unique user id is empty. Set a cached token.");
            }

            ExactOnlineConnector connector = new ExactOnlineConnector(token.AccessToken);

            return(connector);
        }
Exemple #4
0
        public static Task <Auth.AuthenticationResult> GetTokenByAuthCode(NameValueCollection callbackParams)
        {
            var    client   = GetClient();
            string token    = client.GetToken(callbackParams);
            var    userInfo = GetUserInfo(token);

            OAuthToken oauthToken = new OAuthToken()
            {
                AccessToken       = token,
                RefreshToken      = client.RefreshToken,
                ExpiresOnUtcTicks = client.ExpiresAt.Ticks,
                UserName          = userInfo.FullName,
                UserUniqueId      = userInfo.UserID
            };

            var tokenCache = TokenCacheFactory.GetTokenCache();

            tokenCache.CacheToken(oauthToken);

            var result = ConvertAuthenticationResult(oauthToken, tokenCache);

            return(Task.FromResult <Auth.AuthenticationResult>(result));
        }