/
token-grant.service.ts
47 lines (43 loc) · 1.32 KB
/
token-grant.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Injectable } from '@nestjs/common';
import { UserService } from '../../../models/user/user.service';
import { OAuth2TokenGeneratorService } from '../../middlewares/oauth2-token-generator.service';
import { ClientService } from '../../../models/client/client.service';
@Injectable()
export class TokenGrantService {
accessToken: string;
constructor(
private readonly userService: UserService,
private readonly clientService: ClientService,
private readonly tokenGeneratorService: OAuth2TokenGeneratorService,
) {}
async grantToken(client, user, ares, areq, done) {
try {
const localUser = await this.userService.findOne({
email: user.email,
});
const localClient = await this.clientService.findOne({
clientId: areq.clientID,
});
const scope = await this.tokenGeneratorService.getValidScopes(
client,
areq.scope,
);
const [
bearerToken,
extraParams,
] = await this.tokenGeneratorService.getBearerToken(
localClient.clientId,
localUser.uuid,
scope,
false,
);
this.accessToken = bearerToken.accessToken;
return done(null, bearerToken.accessToken, extraParams);
} catch (error) {
return done(error);
}
}
getAccessToken() {
return this.accessToken;
}
}