cnpm / cnpmcore

Private NPM Registry for Enterprise
https://npmmirror.com
MIT License
624 stars 84 forks source link

403 Forbidden - PUT http://127.0.0.1:7001/-/user/org.couchdb.user #718

Open a1528zhang opened 1 month ago

a1528zhang commented 1 month ago

本地环境: mac OS node 20

参考文档 https://github.com/cnpm/cnpmcore/blob/master/INTEGRATE.md,我自己创建了一个项目,然后根据 cnpmcore 的 sql 创建好了数据库,启动正常。 但是在验证的时候无法登录,日志如下

0 verbose cli /Users/az/.nvm/versions/node/v20.16.0/bin/node /Users/az/.nvm/versions/node/v20.16.0/bin/npm
1 info using npm@10.8.1
2 info using node@v20.16.0
3 silly config load:file:/Users/az/.nvm/versions/node/v20.16.0/lib/node_modules/npm/npmrc
4 silly config load:file:/Users/az/.npmrc
5 silly config load:file:/Users/az/.nvm/versions/node/v20.16.0/etc/npmrc
6 verbose title npm login
7 verbose argv "login" "--registry" "http://127.0.0.1:7001"
8 verbose logfile logs-max:10 dir:/Users/az/.npm/_logs/2024-10-22T03_23_50_195Z-
9 verbose logfile /Users/az/.npm/_logs/2024-10-22T03_23_50_195Z-debug-0.log
10 notice Log in on http://127.0.0.1:7001/
11 verbose web login before first POST
12 silly logfile start cleaning logs, removing 1 files
13 silly logfile done cleaning log files
14 http fetch POST 403 http://127.0.0.1:7001/-/v1/login 49ms
15 verbose web login not supported, trying couch
16 verbose login before first PUT {
16 verbose login   _id: 'org.couchdb.user:az',
16 verbose login   name: 'az',
16 verbose login   password: 'XXXXX',
16 verbose login   type: 'user',
16 verbose login   roles: [],
16 verbose login   date: '2024-10-22T03:24:03.550Z'
16 verbose login }
17 http fetch PUT 403 http://127.0.0.1:7001/-/user/org.couchdb.user:az 17ms
18 verbose stack HttpErrorGeneral: 403 Forbidden - PUT http://127.0.0.1:7001/-/user/org.couchdb.user:az
18 verbose stack     at /Users/az/.nvm/versions/node/v20.16.0/lib/node_modules/npm/node_modules/npm-registry-fetch/lib/check-response.js:95:15
18 verbose stack     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
18 verbose stack     at async putCouch (/Users/az/.nvm/versions/node/v20.16.0/lib/node_modules/npm/node_modules/npm-profile/lib/index.js:133:18)
18 verbose stack     at async loginCouch (/Users/az/.nvm/versions/node/v20.16.0/lib/node_modules/npm/node_modules/npm-profile/lib/index.js:177:12)
18 verbose stack     at async otplease (/Users/az/.nvm/versions/node/v20.16.0/lib/node_modules/npm/lib/utils/auth.js:8:12)
18 verbose stack     at async Object.login (/Users/az/.nvm/versions/node/v20.16.0/lib/node_modules/npm/lib/utils/auth.js:91:11)
18 verbose stack     at async Login.exec (/Users/az/.nvm/versions/node/v20.16.0/lib/node_modules/npm/lib/commands/login.js:31:35)
18 verbose stack     at async Npm.exec (/Users/az/.nvm/versions/node/v20.16.0/lib/node_modules/npm/lib/npm.js:207:9)
18 verbose stack     at async module.exports (/Users/az/.nvm/versions/node/v20.16.0/lib/node_modules/npm/lib/cli/entry.js:74:5)
19 verbose statusCode 403
20 error code E403
21 error 403 403 Forbidden - PUT http://127.0.0.1:7001/-/user/org.couchdb.user:az
22 error 403 In most cases, you or one of your dependencies are requesting
22 error 403 a package version that is forbidden by your security policy, or
22 error 403 on a server you do not have access to.
23 verbose cwd /Users/az
24 verbose os Darwin 22.3.0
25 verbose node v20.16.0
26 verbose npm  v10.8.1
27 verbose exit 1
28 verbose code 1
29 error A complete log of this run can be found in: /Users/az/.npm/_logs/2024-10-22T03_23_50_195Z-debug-0.log

怎么解决?

github-actions[bot] commented 1 month ago

我们已经看到你的反馈,如果是功能缺陷,可以提供一下重现该问题的方式;如果是新功能需求,我们会尽快加入讨论。同时我们非常期待你可以加入我们的贡献者行列,让项目可以长期可持续发展。

a1528zhang commented 1 month ago

上面的问题原因是 csrf 防范默认开启: https://www.eggjs.org/zh-CN/core/security#%E5%AE%89%E5%85%A8%E5%A8%81%E8%83%81-csrf-%E7%9A%84%E9%98%B2%E8%8C%83 我在本地完全禁用掉后,不会有 403 的问题了

config.security = {
    xframe: {
      enable: false,
    },
    csrf: {
      // 判断是否需要 ignore 的方法,请求上下文 `context` 作为第一个参数
      ignore: () => true,
    },
  };

但是根据文档,我在调用

  // 触发回调接口,会自动完成用户创建
  await this.httpclient.request(`${ctx.origin}/-/v1/login/sso/${name}`, { method: 'POST' });

# 后端得到日志
2024-10-22 11:54:29,165 INFO 73229 [-/::1/5688cec0-9029-11ef-a58b-37a6fc033f97/1ms POST /-/v1/login/sso/hello] [Tracing] auth: 0, npm-command: -, referer: -, user-agent: "node-urllib/2.44.0 Node.js/20.16.0 (OS X; arm64)"

这行代码成功后,再次执行

npm login --registry=http://127.0.0.1:7001

仍然需要我输入用户名密码,我查看数据库也没有任何数据

elrrrrrrr commented 1 month ago

@a1528zhang 是需要开启 sso 功能吗? 确认下 AuthApdater#ensureCurrentUser 方法是否实现,根据提供的日志没有看到请求成功返回 200 的日志。

预期会在 User 表内创建对应记录

a1528zhang commented 1 month ago

@elrrrrrrr 我只是想要集成到我们自己的项目中,AuthApdater#ensureCurrentUser 是存在的:

async ensureCurrentUser(): Promise<userResult | null> {
    return {
      name: 'hello',
      email: 'hello@cnpmjs.org',
    };
  }

问题我通过 cnpmcore 的代码找到了,你们 INTEGRATE.md 文档里没有提新建用户的事,我在启动项目后通过

PUT http://localhost:7001/-/user/org.couchdb.user:admin

创建用户后,登录就正常了。

然后又有一个问题,我写了AuthAdapter

import { AccessLevel, EggContext, Inject, SingletonProto } from '@eggjs/tegg';
import { AuthAdapter } from 'cnpmcore/infra/AuthAdapter';
import { randomUUID } from 'crypto';
import { EggLogger } from 'egg';
import { AuthUrlResult, userResult } from 'node_modules/cnpmcore/dist/app/common/typing';

const ONE_DAY = 3600 * 24;

@SingletonProto({
  name: 'authAdapter',
  accessLevel: AccessLevel.PUBLIC,
})
export class MyAuthAdapter extends AuthAdapter {

  @Inject()
  private readonly logger: EggLogger;

  async getAuthUrl(ctx: EggContext): Promise<AuthUrlResult> {
    const sessionId = randomUUID();

    console.log('sessionId ', sessionId, ctx.origin); // 这里打印了 session id

    this.logger.info('sessionId %s, %s', sessionId, ctx.origin);
    await this.redis.setex(sessionId, ONE_DAY, '');
    return {
      // 替换实际企业内的登录中心地址,这里我们以系统内默认的 hello 路由为例
      loginUrl: `${ctx.origin}/hello?name=${sessionId}`,
      doneUrl: `${ctx.href}/done/session/${sessionId}`,
    };
  }

  async ensureCurrentUser(): Promise<userResult | null> {
    return {
      name: 'hello',
      email: 'hello@cnpmjs.org',
    };
  }
}

然后我通过命令行

npm login --registry=http://127.0.0.1:7001

进行登录,得到的后端日志

2024-10-22 16:24:46,275 INFO 15753 [-/127.0.0.1/18af6a20-904f-11ef-99ff-9b887dbcba10/4ms POST /-/v1/login] [Tracing] auth: 1, npm-command: login, referer: -, user-agent: "npm/10.8.1 node/v20.16.0 darwin arm64 workspaces/false"

2024-10-22 16:24:58,092 INFO 15753 [-/127.0.0.1/1fba8bb0-904f-11ef-99ff-9b887dbcba10/1ms PUT /-/user/org.couchdb.user:admin] [Tracing] auth: 1, npm-command: login, referer: -, user-agent: "npm/10.8.1 node/v20.16.0 darwin arm64 workspaces/false"

这里并没有打印出 sessionId,就是没有走我写的 AuthAdapter。

但是我通过直接发送请求调用

POST http://localhost:7001/-/v1/login

得到的后端日志

2024-10-22 16:25:17,532 INFO 15753 [-/::1/2b50dab0-904f-11ef-99ff-9b887dbcba10/3ms POST /-/v1/login] [Tracing] auth: 1, npm-command: -, referer: -, user-agent: "Apifox/1.0.0 (https://apifox.com)"

sessionId  567c55c4-5e16-4ff5-94a2-f6d2706f1e8f http://localhost:7001

又打印出了 sessionId, 走了我写的 AuthAdapter。

这两种方式什么区别?调用的应该是同一个后端 api

elrrrrrrr commented 2 weeks ago

https://github.com/elrrrrrrr/examples/tree/cnpmcore-integrate/hello-tegg

问题我通过 cnpmcore 的代码找到了,你们 INTEGRATE.md 文档里没有提新建用户的事,我在启动项目后通过 webauth login 流程会自动触发账号初始化流程的。

@a1528zhang (⊙o⊙)… 看描述比较诡异,没能重现你描述的问题。 我刚提交了一下新的分支,看看能否在这个基础上重现下问题?

image