Exemplo n.º 1
0
 public static async Task<string> GetAccessToken(this IBotContext context, string resourceId)
 {
     AuthResult authResult;
     if (context.UserData.TryGetValue(ContextConstants.AuthResultKey, out authResult))
     {
         try
         {
             InMemoryTokenCacheADAL tokenCache = new InMemoryTokenCacheADAL(authResult.TokenCache);
             var result = await AzureActiveDirectoryHelper.GetToken(authResult.UserUniqueId, tokenCache, resourceId);
             authResult.AccessToken = result.AccessToken;
             authResult.ExpiresOnUtcTicks = result.ExpiresOnUtcTicks;
             authResult.TokenCache = tokenCache.Serialize();
             context.StoreAuthResult(authResult);
         }
         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();
             return null;
         }
         return authResult.AccessToken;
     }
     return null;
 }
Exemplo n.º 2
0
        public static async Task <string> GetADALAccessToken(IBotContext context, AzureADAuthenticationRequest request)
        {
            AuthenticationSettings authenticationSettings = AuthenticationSettings.GetFromAppSettings();
            AuthenticationResult   authenticationResult;

            if (context.UserData.TryGetValue(AuthenticationConstants.AuthResultKey, out authenticationResult))
            {
                try
                {
                    var tokenCache = TokenCacheFactory.SetADALTokenCache(authenticationResult.TokenCache);

                    var result = await AzureActiveDirectoryHelper.GetToken(authenticationResult.UserUniqueId, authenticationSettings, request.ResourceId);

                    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);
        }
Exemplo n.º 3
0
        public static async Task <string> GetAccessToken(this IBotContext context, string resourceId)
        {
            AuthResult authResult;

            if (context.Activity.ChannelId.Equals("cortana", StringComparison.InvariantCultureIgnoreCase))
            {
                string token = null;
                if (context.UserData.TryGetValue(ContextConstants.AuthResultKey, out authResult))
                {
                    //we have credential
                }
                else
                {
                    token = GetCortanaAccessToken(context);
                    var jwt = new JwtSecurityToken(token);
                    if (authResult == null)
                    {
                        authResult = new AuthResult();
                    }

                    authResult.AccessToken = token;
                    long tick = long.MinValue;
                    long.TryParse(jwt.Payload.Claims.Where(c => c.Type.Equals("exp", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault()?.Value, out tick);
                    authResult.ExpiresOnUtcTicks = tick;
                    InMemoryTokenCacheMSAL tokenCache = new InMemoryTokenCacheMSAL(Encoding.ASCII.GetBytes(token));
                    authResult.TokenCache = tokenCache.Serialize();
                    context.StoreAuthResult(authResult);
                }
                return(authResult.AccessToken);
            }
            else
            {
                if (context.UserData.TryGetValue(ContextConstants.AuthResultKey, out authResult))
                {
                    try
                    {
                        InMemoryTokenCacheADAL tokenCache = new InMemoryTokenCacheADAL(authResult.TokenCache);
                        var result = await AzureActiveDirectoryHelper.GetToken(authResult.UserUniqueId, tokenCache, resourceId);

                        authResult.AccessToken       = result.AccessToken;
                        authResult.ExpiresOnUtcTicks = result.ExpiresOnUtcTicks;
                        authResult.TokenCache        = tokenCache.Serialize();
                        context.StoreAuthResult(authResult);
                    }
                    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();

                        return(null);
                    }
                    return(authResult.AccessToken);
                }
                return(null);
            }
        }
Exemplo n.º 4
0
        public static async Task <string> GetAlias(this IBotContext context)
        {
            AuthResult authResult;
            string     validated = null;

            if (context.UserData.TryGetValue(ContextConstants.AuthResultKey, out authResult) &&
                context.UserData.TryGetValue(ContextConstants.MagicNumberValidated, out validated) &&
                validated == "true")
            {
                try
                {
                    if (string.Equals(AuthSettings.Mode, "v2", StringComparison.OrdinalIgnoreCase))
                    {
                        InMemoryTokenCacheMSAL tokenCache = new InMemoryTokenCacheMSAL(authResult.TokenCache);
                        var result = await AzureActiveDirectoryHelper.GetToken(authResult.UserUniqueId, tokenCache, AuthSettings.Scopes);

                        authResult.AccessToken       = result.AccessToken;
                        authResult.ExpiresOnUtcTicks = result.ExpiresOnUtcTicks;
                        authResult.TokenCache        = tokenCache.Serialize();
                        authResult.Alias             = result.Alias;
                        context.StoreAuthResult(authResult);
                    }
                    else if (string.Equals(AuthSettings.Mode, "b2c", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new NotImplementedException();
                    }
                    else if (string.Equals(AuthSettings.Mode, "v1", StringComparison.OrdinalIgnoreCase))
                    {
                        InMemoryTokenCacheADAL tokenCache = new InMemoryTokenCacheADAL(authResult.TokenCache);
                        var result = await AzureActiveDirectoryHelper.GetToken(authResult.UserUniqueId, tokenCache, ConfigurationManager.AppSettings["ActiveDirectory.ResourceId"]);

                        authResult.AccessToken       = result.AccessToken;
                        authResult.ExpiresOnUtcTicks = result.ExpiresOnUtcTicks;
                        authResult.TokenCache        = tokenCache.Serialize();
                        authResult.Alias             = result.Alias;
                        context.StoreAuthResult(authResult);
                    }
                }
                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();

                    return(null);
                }
                return(authResult.Alias.Split('@')[0]);
            }

            return(null);
        }
Exemplo n.º 5
0
        public static async Task <string> GetAccessToken(this IBotContext context, string[] scopes)
        {
            AuthResult authResult;

            if (context.UserData.TryGetValue(ContextConstants.AuthResultKey, out authResult))
            {
                try
                {
                    if (string.Equals(AuthSettings.Mode, "v2", StringComparison.OrdinalIgnoreCase))
                    {
                        InMemoryTokenCacheMSAL tokenCache = new InMemoryTokenCacheMSAL(authResult.TokenCache);

                        var result = await AzureActiveDirectoryHelper.GetToken(authResult.UserUniqueId, tokenCache, scopes);

                        authResult.AccessToken       = result.AccessToken;
                        authResult.ExpiresOnUtcTicks = result.ExpiresOnUtcTicks;
                        authResult.TokenCache        = tokenCache.Serialize();

                        context.StoreAuthResult(authResult);
                    }
                    else if (string.Equals(AuthSettings.Mode, "b2c", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new NotImplementedException();
                    }
                }
                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();

                    return(null);
                }


                return(authResult.AccessToken);
            }

            return(null);
        }