@@ -6,6 +6,8 @@ const originalCommands = require('./commands.js');
66const originalMustache = require ( 'mustache' ) ;
77const util = require ( 'util' ) ;
88const request = require ( 'postman-request' ) ;
9+ const { randomBytes } = require ( 'crypto' ) ;
10+ const http = require ( 'http' ) ;
911
1012class VaultError extends Error { }
1113
@@ -244,5 +246,52 @@ module.exports = (config = {}) => {
244246 const assignFunctions = ( commandName ) => generateFunction ( commandName , commands [ commandName ] ) ;
245247 Object . keys ( commands ) . forEach ( assignFunctions ) ;
246248
249+ client [ 'oidcFlow' ] = ( ) => import ( 'open' )
250+ . then ( ( { default : open } ) => {
251+ const oidcCallbackPath = '/oidc/callback' ;
252+ const serverConfig = {
253+ host : 'localhost' ,
254+ port : 8250 ,
255+ protocol : 'http'
256+ }
257+ return new Promise ( ( done , reject ) => {
258+ const client_nonce = randomBytes ( 20 ) . toString ( 'hex' ) . slice ( 20 ) ;
259+
260+ const server = http . createServer ( ( req , res ) => {
261+ const responseUrl = new URL ( req . url , `${ serverConfig . protocol } ://${ serverConfig . host } ` )
262+ if ( responseUrl . pathname === oidcCallbackPath ) {
263+ res . write ( 'Signed in via your OIDC provider\nYou can now close this window and start using Vault.' ) ;
264+ res . end ( ) ;
265+ const code = responseUrl . searchParams . get ( 'code' )
266+ const state = responseUrl . searchParams . get ( 'state' )
267+ client . oidcCallback ( {
268+ state,
269+ code,
270+ client_nonce,
271+ } )
272+ . then ( ( ) => {
273+ server . close ( done ) ;
274+ } )
275+ . catch ( reject )
276+ }
277+ if ( ! res . writableEnded ) {
278+ res . end ( ) ;
279+ }
280+ } ) ;
281+
282+ server . listen ( serverConfig . port , serverConfig . host , ( ) => { } ) ;
283+
284+ client . oidcAuthUrl ( {
285+ redirect_uri : `${ serverConfig . protocol } ://${ serverConfig . host } :${ serverConfig . port } ${ oidcCallbackPath } ` ,
286+ client_nonce,
287+ } )
288+ . then ( ( r ) => {
289+ console . log ( `Complete the login via your OIDC provider. Launching browser to: \n\n${ r . data . auth_url } \n\n if browser does not open automatically, please copy paste the above URL` )
290+ open ( r . data . auth_url )
291+ } )
292+ . catch ( reject )
293+ } )
294+ } )
295+
247296 return client ;
248297} ;
0 commit comments