Skip to content

Commit

Permalink
fix: update access token API (#973)
Browse files Browse the repository at this point in the history
* fix

* fix
  • Loading branch information
RiXelanya authored Oct 10, 2024
1 parent 2a42f27 commit f343f73
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@ jobs:
run: |
sed -i "s|myriadsocial/myriad-api:.*|myriadsocial/myriad-api:${{ github.sha }}|" /home/$USER/docker-compose.yml
- name: Deploy Updated Compose
run: sudo systemctl restart myriad-social.service
run: sudo systemctl restart myriad-social.service
31 changes: 0 additions & 31 deletions src/controllers/user/personal-access-token.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,6 @@ export class UserPersonalAccessTokenController {
return this.userService.createAccessToken(data);
}

@get('/user/personal-admin-access-tokens')
@response(200, {
description: 'CREATE user personal-admin-access-tokens',
content: {
'application/json': {schema: getModelSchemaRef(UserPersonalAccessToken)},
},
})
async generate(): Promise<UserPersonalAccessToken> {
return this.userService.createAdminToken();
}

@get('/user/personal-access-tokens')
@response(200, {
description: 'GET user personal-access-token',
Expand All @@ -76,26 +65,6 @@ export class UserPersonalAccessTokenController {
return this.userService.accessTokens();
}

@patch('/user/personal-access-tokens/{id}')
@response(204, {
description: 'UPDATE user personal-access-token scopes',
})
async updateById(
@param.path.string('id') id: string,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(UpdateUserPersonalAccessTokenDto, {
partial: true,
}),
},
},
})
data: Partial<UpdateUserPersonalAccessTokenDto>,
): Promise<Count> {
return this.userService.updateAccessTokenScopes(id, data);
}

@del('/user/personal-access-tokens/{id}')
@response(204, {
description: 'REMOVE user personal-access-token',
Expand Down
16 changes: 4 additions & 12 deletions src/models/user-personal-access-token.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ export class UserPersonalAccessToken extends Entity {
type: 'string',
required: true,
})
description: string;

@property({
type: 'array',
itemType: 'string',
required: true,
})
scopes: string[];
hash: string;

@property({
type: 'date',
Expand Down Expand Up @@ -78,14 +71,13 @@ export class CreateUserPersonalAccessTokenDto extends Model {
type: 'string',
required: true,
})
description: string;
token: string;

@property({
type: 'array',
itemType: 'string',
type: 'string',
required: true,
})
scopes: string[];
hash: string;

constructor(data: Partial<CreateUserPersonalAccessTokenDto>) {
super(data);
Expand Down
5 changes: 3 additions & 2 deletions src/services/authentication/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {CurrencyService} from '../currency.service';
import {MetricService} from '../metric.service';
import {validateAccount} from '../../utils/validate-account';
import NonceGenerator from 'a-nonce-generator';
import {sha256} from 'js-sha256';

@injectable({scope: BindingScope.TRANSIENT})
export class AuthService {
Expand Down Expand Up @@ -431,10 +432,10 @@ export class AuthService {
public async loginByPAT(requestLogin: RequestLoginByPAT): Promise<UserToken> {
const {token} = requestLogin;
let user: User | null = null;
const hash = sha256(token);
const validPAT = await this.userPersonalAccessTokenRepository.find({
where: {
description: 'Admin Personal Access Token',
id: token,
hash: hash,
},
});
if (!validPAT) {
Expand Down
61 changes: 1 addition & 60 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,6 @@ export class UserService {
public async createAccessToken(
data: CreateUserPersonalAccessTokenDto,
): Promise<UserPersonalAccessToken> {
if (data.description === 'Admin Personal Access Token') {
throw new HttpErrors.UnprocessableEntity(
'The description you used is reserved for internal use. Try another description',
);
}
if (data.scopes.includes('Admin')) {
throw new HttpErrors.UnprocessableEntity(
'Scopes containing Admin is forbidden for this method',
);
}
const accessToken = await this.jwtService.generateToken(this.currentUser);
const user = await this.userRepository.findById(
this.currentUser[securityId],
);
Expand All @@ -320,63 +309,15 @@ export class UserService {
}
const pat = new UserPersonalAccessToken({
...data,
token: accessToken,
userId: user.id,
});

return this.userPersonalAccessTokenRepository.create(pat);
}

public async createAdminToken(): Promise<UserPersonalAccessToken> {
const filter: Where<UserPersonalAccessToken> = {
userId: this.currentUser[securityId],
description: 'Admin Personal Access Token',
};
const data = {
description: 'Admin Personal Access Token',
scopes: ['Admin'],
};
await this.userPersonalAccessTokenRepository.deleteAll(filter);
const accessToken = await this.jwtService.generateToken(this.currentUser);
const user = await this.userRepository.findById(
this.currentUser[securityId],
);
if (!user) {
throw new HttpErrors.UnprocessableEntity('Unauthorized');
}
const pat = new UserPersonalAccessToken({
...data,
token: accessToken,
userId: user.id,
});

return this.userPersonalAccessTokenRepository.create(pat);
}

public async updateAccessTokenScopes(
id: string,
data: Partial<UpdateUserPersonalAccessTokenDto>,
): Promise<Count> {
if (data.description === 'Admin Personal Access Token') {
throw new HttpErrors.UnprocessableEntity(
'The description you used is reserved for internal use. Try another description',
);
}
if (data?.scopes?.includes('Admin')) {
throw new HttpErrors.UnprocessableEntity(
'Scopes containing Admin is forbidden for this method',
);
}
if (!data?.scopes) return {count: 0};
return this.userPersonalAccessTokenRepository.updateAll(data, {
id,
userId: this.currentUser[securityId],
});
}

public async removeAccessToken(id: string): Promise<Count> {
return this.userPersonalAccessTokenRepository.deleteAll({
id,
token: id,
userId: this.currentUser[securityId],
});
}
Expand Down

0 comments on commit f343f73

Please sign in to comment.