@@ -6,27 +6,31 @@ import { Buffer } from 'node:buffer';
66import { logger } from '@redocly/openapi-core' ;
77import { type Credentials , RedoclyOAuthDeviceFlow } from './device-flow.js' ;
88
9- const SALT = '4618dbc9-8aed-4e27-aaf0-225f4603e5a4' ;
9+ const CREDENTIAL_SALT = '4618dbc9-8aed-4e27-aaf0-225f4603e5a4' ;
1010const CRYPTO_ALGORITHM = 'aes-256-cbc' ;
1111
1212export class RedoclyOAuthClient {
13- public static readonly CREDENTIALS_FILE = 'credentials' ;
13+ public readonly credentialsFolderPath : string ;
14+ public readonly credentialsFilePath : string ;
15+ public readonly credentialsFileName : string ;
1416
15- private readonly dir : string ;
1617 private readonly key : Buffer ;
1718 private readonly iv : Buffer ;
1819
1920 constructor ( ) {
2021 const homeDirPath = homedir ( ) ;
2122
22- this . dir = path . join ( homeDirPath , '.redocly' ) ;
23- mkdirSync ( this . dir , { recursive : true } ) ;
23+ this . credentialsFolderPath = path . join ( homeDirPath , '.redocly' ) ;
24+ this . credentialsFileName = 'credentials' ;
25+ this . credentialsFilePath = path . join ( this . credentialsFolderPath , this . credentialsFileName ) ;
2426
25- this . key = crypto . createHash ( 'sha256' ) . update ( `${ homeDirPath } ${ SALT } ` ) . digest ( ) ; // 32-byte key
26- this . iv = crypto . createHash ( 'md5' ) . update ( homeDirPath ) . digest ( ) ; // 16-byte IV
27+ this . key = crypto . createHash ( 'sha256' ) . update ( `${ homeDirPath } ${ CREDENTIAL_SALT } ` ) . digest ( ) ;
28+ this . iv = crypto . createHash ( 'md5' ) . update ( homeDirPath ) . digest ( ) ;
29+
30+ mkdirSync ( this . credentialsFolderPath , { recursive : true } ) ;
2731 }
2832
29- public async login ( baseUrl : string ) {
33+ public async login ( baseUrl : string ) : Promise < void > {
3034 const deviceFlow = new RedoclyOAuthDeviceFlow ( baseUrl ) ;
3135
3236 const credentials = await deviceFlow . run ( ) ;
@@ -79,35 +83,31 @@ export class RedoclyOAuthClient {
7983 }
8084 } ;
8185
82- private get credentialsPath ( ) {
83- return path . join ( this . dir , RedoclyOAuthClient . CREDENTIALS_FILE ) ;
84- }
85-
8686 private async saveCredentials ( credentials : Credentials ) : Promise < void > {
8787 try {
8888 const encryptedCredentials = this . encryptCredentials ( credentials ) ;
89- writeFileSync ( this . credentialsPath , encryptedCredentials , 'utf8' ) ;
89+ writeFileSync ( this . credentialsFilePath , encryptedCredentials , 'utf8' ) ;
9090 } catch ( error ) {
9191 logger . error ( `Failed to save credentials: ${ error . message } ` ) ;
9292 }
9393 }
9494
9595 private async readCredentials ( ) : Promise < Credentials | null > {
96- if ( ! existsSync ( this . credentialsPath ) ) {
96+ if ( ! existsSync ( this . credentialsFilePath ) ) {
9797 return null ;
9898 }
9999
100100 try {
101- const encryptedCredentials = readFileSync ( this . credentialsPath , 'utf8' ) ;
101+ const encryptedCredentials = readFileSync ( this . credentialsFilePath , 'utf8' ) ;
102102 return this . decryptCredentials ( encryptedCredentials ) ;
103103 } catch {
104104 return null ;
105105 }
106106 }
107107
108108 private async removeCredentials ( ) : Promise < void > {
109- if ( existsSync ( this . credentialsPath ) ) {
110- rmSync ( this . credentialsPath ) ;
109+ if ( existsSync ( this . credentialsFilePath ) ) {
110+ rmSync ( this . credentialsFilePath ) ;
111111 }
112112 }
113113
0 commit comments