@@ -4,7 +4,7 @@ import { createHash } from 'crypto';
44import { Inject , Service } from 'typedi' ;
55
66import { diConstants } from '@bonadocs/di' ;
7- import { BonadocsLogger } from '@bonadocs/logger' ;
7+ import type { BonadocsLogger } from '@bonadocs/logger' ;
88
99import { ConfigService } from '../configuration/config.service' ;
1010import { ApplicationError , applicationErrorCodes } from '../errors/ApplicationError' ;
@@ -16,6 +16,8 @@ import {
1616 LoginUserResponse ,
1717 RefreshTokenRequest ,
1818 RefreshTokenResponse ,
19+ WSTokenRequest ,
20+ WSTokenResponse ,
1921} from './auth.interface' ;
2022import { AuthSource } from './auth.types' ;
2123import { FirebaseJWTProvider } from './firebase' ;
@@ -87,6 +89,30 @@ export class AuthService {
8789 } ;
8890 }
8991
92+ async wsLogin ( request : WSTokenRequest ) : Promise < WSTokenResponse > {
93+ const isValid = this . validateJWT ( request . token ) ;
94+ if ( ! isValid ) {
95+ throw new ApplicationError ( {
96+ logger : this . logger ,
97+ message : 'Api token provided not valid' ,
98+ errorCode : applicationErrorCodes . unauthorized ,
99+ } ) ;
100+ }
101+ const payload = JSON . parse ( Buffer . from ( request . token . split ( '.' ) [ 1 ] , 'base64url' ) . toString ( ) ) ;
102+ const validityPeriod = this . validityFromEnv ? Number ( this . validityFromEnv ) : 6 * 3600 ;
103+ const token = this . generateJWT (
104+ {
105+ purpose : 'ws-api' ,
106+ sub : payload . userId ,
107+ } ,
108+ validityPeriod ,
109+ ) ;
110+
111+ return {
112+ token,
113+ } ;
114+ }
115+
90116 getHandler ( logger : BonadocsLogger , authSource : AuthSource ) : AuthHandler {
91117 const func = this . authSourceHandlers [ authSource ] ;
92118 if ( ! func || typeof func !== 'function' ) {
@@ -206,6 +232,13 @@ export class AuthService {
206232 }
207233
208234 const payload = JSON . parse ( Buffer . from ( token . split ( '.' ) [ 1 ] , 'base64url' ) . toString ( ) ) ;
235+ if ( payload . purpose === 'ws-api' ) {
236+ throw new ApplicationError ( {
237+ message : 'Invalid JWT - purpose ws server' ,
238+ logger : this . logger ,
239+ errorCode : applicationErrorCodes . unauthorized ,
240+ } ) ;
241+ }
209242 if ( ! payload . userId ) {
210243 throw new ApplicationError ( {
211244 message : 'Invalid JWT - missing user ID' ,
0 commit comments