Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Use acct:URL for access token
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 30, 2020
1 parent 6e03f2b commit 373f40d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/modules/auth/auth.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Request as ExpressRequest } from 'express';
export type MfaMethod = 'NONE' | 'SMS' | 'TOTP' | 'EMAIL';

export interface AccessTokenClaims {
id: number;
sub: string;
scopes: string[];
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ export class AuthService {
private async getAccessToken(user: User): Promise<string> {
const scopes = await this.getScopes(user);
const payload: AccessTokenClaims = {
id: user.id,
sub: `acct:${user.id}@${this.configService.get('security.issuerDomain')}`,
scopes,
};
return this.tokensService.signJwt(
Expand Down
13 changes: 10 additions & 3 deletions src/modules/auth/staart.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { Request } from 'express';
import ipRangeCheck from 'ip-range-check';
import minimatch from 'minimatch';
import { Strategy } from 'passport-strategy';
import { getClientIp } from 'request-ip';
import { ApiKeysService } from '../api-keys/api-keys.service';
import { validate } from 'uuid';
import { LOGIN_ACCESS_TOKEN } from '../../providers/tokens/tokens.constants';
import { TokensService } from '../../providers/tokens/tokens.service';
import { ApiKeysService } from '../api-keys/api-keys.service';
import { AccessTokenClaims, AccessTokenParsed } from './auth.interface';
import { validate } from 'uuid';

class StaartStrategyName extends Strategy {
name = 'staart';
Expand All @@ -20,6 +21,7 @@ export class StaartStrategy extends PassportStrategy(StaartStrategyName) {
constructor(
private apiKeyService: ApiKeysService,
private tokensService: TokensService,
private configService: ConfigService,
) {
super();
}
Expand Down Expand Up @@ -79,7 +81,12 @@ export class StaartStrategy extends PassportStrategy(StaartStrategyName) {
LOGIN_ACCESS_TOKEN,
bearerToken,
) as AccessTokenClaims;
const { id, scopes } = payload;
const { sub, scopes } = payload;
const [userPart, hostPart] = sub.split('@');
if (hostPart !== this.configService.get('security.issuerDomain'))
throw new Error('Invalid issuer domain');
const id = parseInt(userPart.replace('acct:', ''));
if (isNaN(id)) throw new Error('Invalid user ID');
return this.safeSuccess({ type: 'user', id, scopes });
} catch (error) {}

Expand Down
18 changes: 10 additions & 8 deletions src/providers/tokens/tokens.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,32 @@ export class TokensService {
constructor(private configService: ConfigService) {}

signJwt(
subject: string,
payload: number | string | object | Buffer,
jwtType: string,
payload: object,
expiresIn?: string,
options?: SignOptions,
) {
if (typeof payload === 'number') payload = payload.toString();
return sign(
payload,
{ ...payload, typ: jwtType },
this.configService.get<string>('security.jwtSecret') ?? '',
{
...options,
subject,
expiresIn,
},
);
}

verify<T>(subject: string, token: string, options?: VerifyOptions) {
verify<T>(jwtType: string, token: string, options?: VerifyOptions) {
try {
return (verify(
const result = (verify(
token,
this.configService.get<string>('security.jwtSecret') ?? '',
{ ...options, subject },
options,
) as any) as T;
if ('typ' in result) {
if ((result as { typ?: string }).typ !== jwtType) throw new Error();
} else throw new Error();
return result;
} catch (error) {
throw new UnauthorizedException(INVALID_TOKEN);
}
Expand Down

0 comments on commit 373f40d

Please sign in to comment.