@@ -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,46 @@ 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+ return new Promise ( ( done , reject ) => {
253+ const client_nonce = randomBytes ( 20 ) . toString ( 'hex' ) . slice ( 20 ) ;
254+
255+ const server = http . createServer ( ( req , res ) => {
256+ const responseUrl = new URL ( `http://localhost${ req . url } ` )
257+ if ( responseUrl . pathname === oidcCallbackPath ) {
258+ res . write ( 'Signed in via your OIDC provider\nYou can now close this window and start using Vault.' ) ;
259+ res . end ( ) ;
260+ const code = responseUrl . searchParams . get ( 'code' )
261+ const state = responseUrl . searchParams . get ( 'state' )
262+ client . oidcCallback ( {
263+ state,
264+ code,
265+ client_nonce,
266+ } )
267+ . then ( ( ) => {
268+ server . close ( done ) ;
269+ } )
270+ . catch ( reject )
271+ }
272+ if ( ! res . writableEnded ) {
273+ res . end ( ) ;
274+ }
275+ } ) ;
276+
277+ server . listen ( 8250 , 'localhost' , ( ) => { } ) ;
278+
279+ client . oidcAuthUrl ( {
280+ redirect_uri : `http://localhost:8250${ oidcCallbackPath } ` ,
281+ client_nonce,
282+ } )
283+ . then ( ( r ) => {
284+ open ( r . data . auth_url )
285+ } )
286+ . catch ( reject )
287+ } )
288+ } )
289+
247290 return client ;
248291} ;
0 commit comments