Skip to content

Commit

Permalink
feat: export getUserAndToken (#480)
Browse files Browse the repository at this point in the history
> Export `getUserAndToken` method for authorization parsing in integrate
mode.

1. 🧶 TokenService adds getUserAndToken method

----------

> 暴露 `getUserAndToken` 方法进行 authorization 解析,面向集成模式的场景
1. 🧶 TokenService 新增 `getUserAndToken` 方法

---------

Co-authored-by: fengmk2 <fengmk2@gmail.com>
  • Loading branch information
elrrrrrrr and fengmk2 authored May 29, 2023
1 parent 1b89b64 commit 6624a36
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
name: Release
on:
# 合并后自动发布
push:
branches: [ master, main ]

# 手动发布
workflow_dispatch: {}
branches: [ master ]

jobs:
release:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
Expand Down
14 changes: 14 additions & 0 deletions app/core/service/TokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { ModelConvertor } from '../../../app/repository/util/ModelConvertor';
import { Package as PackageEntity } from '../entity/Package';
import { ForbiddenError, UnauthorizedError } from 'egg-errors';
import { getScopeAndName } from '../../../app/common/PackageUtil';
import { sha512 } from '../../../app/common/UserUtil';
import { UserRepository } from '../../../app/repository/UserRepository';

@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
Expand All @@ -22,6 +24,8 @@ export class TokenService extends AbstractService {
private readonly TokenPackage: typeof TokenPackageModel;
@Inject()
private readonly Package: typeof PackageModel;
@Inject()
private readonly userRepository: UserRepository;

public async listTokenPackages(token: Token) {
if (isGranularToken(token)) {
Expand Down Expand Up @@ -67,4 +71,14 @@ export class TokenService extends AbstractService {

}

async getUserAndToken(authorization: string) {
if (!authorization) return null;
const matchs = /^Bearer ([\w\.]+?)$/.exec(authorization);
if (!matchs) return null;
const tokenValue = matchs[1];
const tokenKey = sha512(tokenValue);
const authorizedUserAndToken = await this.userRepository.findUserAndTokenByTokenKey(tokenKey);
return authorizedUserAndToken;
}

}
1 change: 1 addition & 0 deletions app/core/service/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,5 @@ export class UserService extends AbstractService {
await this.userRepository.removeCredential(credential.wancId);
}
}

}
22 changes: 7 additions & 15 deletions app/port/UserRoleManager.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {
AccessLevel,
ContextProto,
Inject,
EggContext,
ContextProto,
} from '@eggjs/tegg';
import { EggAppConfig, EggLogger } from 'egg';
import { UnauthorizedError, ForbiddenError } from 'egg-errors';
import { UserRepository } from '../repository/UserRepository';
import { PackageRepository } from '../repository/PackageRepository';
import { Package as PackageEntity } from '../core/entity/Package';
import { User as UserEntity } from '../core/entity/User';
import { Token as TokenEntity } from '../core/entity/Token';
import { sha512 } from '../common/UserUtil';
import { getScopeAndName } from '../common/PackageUtil';
import { RegistryManagerService } from '../core/service/RegistryManagerService';
import { TokenService } from '../core/service/TokenService';
Expand All @@ -24,8 +22,6 @@ export type TokenRole = 'read' | 'publish' | 'setting';
accessLevel: AccessLevel.PRIVATE,
})
export class UserRoleManager {
@Inject()
private readonly userRepository: UserRepository;
@Inject()
private readonly packageRepository: PackageRepository;
@Inject()
Expand Down Expand Up @@ -108,20 +104,16 @@ export class UserRoleManager {
user: this.currentAuthorizedUser,
};
}

this.handleAuthorized = true;
const authorization = ctx.get('authorization');
if (!authorization) return null;
const matchs = /^Bearer ([\w\.]+?)$/.exec(authorization);
if (!matchs) return null;
const tokenValue = matchs[1];
const tokenKey = sha512(tokenValue);
const authorizedUserAndToken = await this.userRepository.findUserAndTokenByTokenKey(tokenKey);
if (authorizedUserAndToken) {
this.currentAuthorizedToken = authorizedUserAndToken.token;
this.currentAuthorizedUser = authorizedUserAndToken.user;
ctx.userId = authorizedUserAndToken.user.userId;
const authorizedUserAndToken = await this.tokenService.getUserAndToken(authorization);
if (!authorizedUserAndToken) {
return null;
}
this.currentAuthorizedToken = authorizedUserAndToken.token;
this.currentAuthorizedUser = authorizedUserAndToken.user;
ctx.userId = authorizedUserAndToken.user.userId;
return authorizedUserAndToken;
}

Expand Down

0 comments on commit 6624a36

Please sign in to comment.