@@ -3,7 +3,7 @@ import { ClientRequest } from 'node:http';
33
44import ghauth from 'ghauth' ;
55
6- import { clearCachedConfig , getMergedConfig , getNcurcPath } from './config.js' ;
6+ import { clearCachedConfig , encryptValue , getMergedConfig , getNcurcPath } from './config.js' ;
77
88export default lazy ( auth ) ;
99
@@ -60,68 +60,90 @@ function encode(name, token) {
6060 return Buffer . from ( `${ name } :${ token } ` ) . toString ( 'base64' ) ;
6161}
6262
63+ function setOwnProperty ( target , key , value ) {
64+ return Object . defineProperty ( target , key , {
65+ __proto__ : null ,
66+ configurable : true ,
67+ enumerable : true ,
68+ value
69+ } ) ;
70+ }
71+
6372// TODO: support jenkins only...or not necessary?
6473// TODO: make this a class with dependency (CLI) injectable for testing
6574async function auth (
6675 options = { github : true } ,
6776 githubAuth = ghauth ) {
68- const result = { } ;
77+ const result = {
78+ get github ( ) {
79+ let username ;
80+ let token ;
81+ try {
82+ ( { username, token } = getMergedConfig ( ) ) ;
83+ } catch ( e ) {
84+ // Ignore error and prompt
85+ }
86+
87+ check ( username , token ) ;
88+ const github = encode ( username , token ) ;
89+ setOwnProperty ( result , 'github' , github ) ;
90+ return github ;
91+ } ,
92+
93+ get jenkins ( ) {
94+ const { username, jenkins_token } = getMergedConfig ( ) ;
95+ if ( ! username || ! jenkins_token ) {
96+ errorExit (
97+ 'Get your Jenkins API token in https://ci.nodejs.org/me/security ' +
98+ 'and run the following command to add it to your ncu config: ' +
99+ 'ncu-config --global set -x jenkins_token'
100+ ) ;
101+ } ;
102+ check ( username , jenkins_token ) ;
103+ const jenkins = encode ( username , jenkins_token ) ;
104+ setOwnProperty ( result , 'jenkins' , jenkins ) ;
105+ return jenkins ;
106+ } ,
107+
108+ get h1 ( ) {
109+ const { h1_username, h1_token } = getMergedConfig ( ) ;
110+ check ( h1_username , h1_token ) ;
111+ const h1 = encode ( h1_username , h1_token ) ;
112+ setOwnProperty ( result , 'h1' , h1 ) ;
113+ return h1 ;
114+ }
115+ } ;
69116 if ( options . github ) {
70- let username ;
71- let token ;
117+ let config ;
72118 try {
73- ( { username , token } = getMergedConfig ( ) ) ;
74- } catch ( e ) {
75- // Ignore error and prompt
119+ config = getMergedConfig ( ) ;
120+ } catch {
121+ config = { } ;
76122 }
77-
78- if ( ! username || ! token ) {
123+ if ( ! Object . hasOwn ( config , 'token' ) || ! Object . hasOwn ( config , 'username' ) ) {
79124 process . stdout . write (
80125 'If this is your first time running this command, ' +
81126 'follow the instructions to create an access token' +
82127 '. If you prefer to create it yourself on Github, ' +
83128 'see https://github.com/nodejs/node-core-utils/blob/main/README.md.\n' ) ;
84129 const credentials = await tryCreateGitHubToken ( githubAuth ) ;
85- username = credentials . user ;
86- token = credentials . token ;
130+ const username = credentials . user ;
131+ let token ;
132+ try {
133+ token = await encryptValue ( credentials . token ) ;
134+ } catch ( err ) {
135+ console . warn ( 'Failed encrypt token, storing unencrypted instead' ) ;
136+ token = credentials . token ;
137+ }
87138 const json = JSON . stringify ( { username, token } , null , 2 ) ;
88139 fs . writeFileSync ( getNcurcPath ( ) , json , {
89140 mode : 0o600 /* owner read/write */
90141 } ) ;
91142 // Try again reading the file
92143 clearCachedConfig ( ) ;
93- ( { username, token } = getMergedConfig ( ) ) ;
94144 }
95- check ( username , token ) ;
96- result . github = encode ( username , token ) ;
97145 }
98146
99- if ( options . jenkins ) {
100- const { username, jenkins_token } = getMergedConfig ( ) ;
101- if ( ! username || ! jenkins_token ) {
102- errorExit (
103- 'Get your Jenkins API token in https://ci.nodejs.org/me/configure ' +
104- 'and run the following command to add it to your ncu config: ' +
105- 'ncu-config --global set jenkins_token TOKEN'
106- ) ;
107- } ;
108- check ( username , jenkins_token ) ;
109- result . jenkins = encode ( username , jenkins_token ) ;
110- }
111-
112- if ( options . h1 ) {
113- const { h1_username, h1_token } = getMergedConfig ( ) ;
114- if ( ! h1_username || ! h1_token ) {
115- errorExit (
116- 'Get your HackerOne API token in ' +
117- 'https://docs.hackerone.com/organizations/api-tokens.html ' +
118- 'and run the following command to add it to your ncu config: ' +
119- 'ncu-config --global set h1_token TOKEN or ' +
120- 'ncu-config --global set h1_username USERNAME'
121- ) ;
122- } ;
123- result . h1 = encode ( h1_username , h1_token ) ;
124- }
125147 return result ;
126148}
127149
0 commit comments