放大和有角度的安全关注 - Cognito

2023-12-28

我试图弄清楚 Cognito 是否需要放大才能与前端一起使用,或者是否有更安全的设置方法。我问的原因是因为我有一个解决方案,允许我使用 cognito、amplify 和 Angular 登录我的应用程序,但为了使其正常工作,我必须将 userPoolId 和 userPoolWebClientId 等内容放入我的 auth.service 中。 .ts 文件。从安全的角度来看,我觉得这在前端是一件非常糟糕的事情。通常我会认为这种敏感信息会存储在后端,但我还没有看到不这样做的解决方案。

我的角度应用程序在 auth.service.ts 中进行了这样的设置

Amplify.configure({
  Auth: {
    region: 'us-east-1',
    userPoolId: '<my user pool id was here. Seems insecure>',
    userPoolWebClientId: '<my user pool id was here. Seems insecure>',
    mandatorySignIn: false,

    oauth: {
      domain: 'mysite.auth.us-east-1.amazoncognito.com',
      scope: ['email', 'profile', 'openid'],
      redirectSignIn: 'http://localhost:4200/',
      redirectSignOut: 'http://localhost:4200/',
      responseType: 'code'
    }
  }
});


const POOL_DATA = {
  UserPoolId: '<my user pool id was here. Seems insecure>',
  ClientId: '<my client id was here. Seems insecure>'
};

const userPool = new CognitoUserPool(POOL_DATA);
...

我有什么遗漏的吗?有没有更安全的方法在前端执行此操作?


如果没有池 ID 和 Web 客户端 ID,则无法选择在前端使用 AWS Cognito。

我建议您不要将凭据保留在服务文件中,而是将其保留在environment.ts 文件中。

  1. 将凭据添加到environment.ts

    export const environment = {
       production: false,
       envName: 'dev',
       cognitoUserPoolId: <secure credential>,
       cognitoClientId: <secure credential>
    }
    
  2. 创建 config.ts 文件作为抽象层。

     import { environment } from '<your folder path>/environments/environment';
    
     export const CONFIG = {
        UserPoolId: environment.cognitoUserPoolId,
        ClientId: environment.cognitoClientId,
     }
    
  3. 在 auth.service.ts 中导入配置文件

     import { CONFIG } from '<your folder path>/config';
    
     Amplify.configure({
        Auth: {
          region: 'us-east-1',
          userPoolId: CONFIG.UserPoolId,
          userPoolWebClientId: CONFIG.ClientId,
          mandatorySignIn: false
       }
     });
    

这样凭证就不会暴露给外界。希望这是有道理的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

放大和有角度的安全关注 - Cognito 的相关文章