99// CLI, we can remove this, and fold the lib/auth/legacy.js back into
1010// lib/adduser.js
1111
12+ const { promisify } = require ( 'util' )
13+
1214const log = require ( 'npmlog' )
13- const npm = require ( '../ npm.js ' )
15+ const profile = require ( 'npm-profile ' )
1416const npmFetch = require ( 'npm-registry-fetch' )
15- const output = require ( '../utils/output.js' )
16- const { promisify } = require ( 'util ' )
17+
18+ const npm = require ( '../npm.js ' )
1719const openUrl = promisify ( require ( '../utils/open-url.js' ) )
1820const otplease = require ( '../utils/otplease.js' )
19- const profile = require ( 'npm-profile' )
20-
21- module . exports . login = function login ( creds , registry , scope , cb ) {
22- log . warn ( 'deprecated' , 'SSO --auth-type is deprecated' )
23- const opts = { ...npm . flatOptions , creds, registry, scope }
24- const ssoType = opts . ssoType
25- if ( ! ssoType ) { return cb ( new Error ( 'Missing option: sso-type' ) ) }
2621
27- // We're reusing the legacy login endpoint, so we need some dummy
28- // stuff here to pass validation. They're never used.
29- const auth = {
30- username : 'npm_' + ssoType + '_auth_dummy_user' ,
31- password : 'placeholder' ,
32- 33- authType : ssoType
34- }
35-
36- otplease ( opts ,
37- opts => profile . loginCouch ( auth . username , auth . password , opts )
38- ) . then ( ( { token, sso } ) => {
39- if ( ! token ) { throw new Error ( 'no SSO token returned' ) }
40- if ( ! sso ) { throw new Error ( 'no SSO URL returned by services' ) }
41- return openUrl ( sso , 'to complete your login please visit' ) . then ( ( ) => {
42- return pollForSession ( registry , token , opts )
43- } ) . then ( username => {
44- log . info ( 'adduser' , 'Authorized user %s' , username )
45- var scopeMessage = scope ? ' to scope ' + scope : ''
46- output ( 'Logged in as %s%s on %s.' , username , scopeMessage , registry )
47- return { token }
48- } )
49- } ) . then ( res => cb ( null , res ) , cb )
50- }
51-
52- function pollForSession ( registry , token , opts ) {
22+ const pollForSession = ( { registry, token, opts } ) => {
5323 log . info ( 'adduser' , 'Polling for validated SSO session' )
5424 return npmFetch . json (
5525 '/-/whoami' , { ...opts , registry, forceAuth : { token } }
@@ -58,7 +28,7 @@ function pollForSession (registry, token, opts) {
5828 err => {
5929 if ( err . code === 'E401' ) {
6030 return sleep ( opts . ssoPollFrequency ) . then ( ( ) => {
61- return pollForSession ( registry , token , opts )
31+ return pollForSession ( { registry, token, opts } )
6232 } )
6333 } else {
6434 throw err
@@ -70,3 +40,46 @@ function pollForSession (registry, token, opts) {
7040function sleep ( time ) {
7141 return new Promise ( ( resolve ) => setTimeout ( resolve , time ) )
7242}
43+
44+ const login = async ( { creds, registry, scope } ) => {
45+ log . warn ( 'deprecated' , 'SSO --auth-type is deprecated' )
46+
47+ const opts = { ...npm . flatOptions , creds, registry, scope }
48+ const { ssoType } = opts
49+
50+ if ( ! ssoType ) {
51+ throw new Error ( 'Missing option: sso-type' )
52+ }
53+
54+ // We're reusing the legacy login endpoint, so we need some dummy
55+ // stuff here to pass validation. They're never used.
56+ const auth = {
57+ username : 'npm_' + ssoType + '_auth_dummy_user' ,
58+ password : 'placeholder' ,
59+ 60+ authType : ssoType
61+ }
62+
63+ const { token, sso } = await otplease ( opts ,
64+ opts => profile . loginCouch ( auth . username , auth . password , opts )
65+ )
66+
67+ if ( ! token ) { throw new Error ( 'no SSO token returned' ) }
68+ if ( ! sso ) { throw new Error ( 'no SSO URL returned by services' ) }
69+
70+ await openUrl ( sso , 'to complete your login please visit' )
71+
72+ const username = await pollForSession ( { registry, token, opts } )
73+
74+ log . info ( 'adduser' , `Authorized user ${ username } ` )
75+
76+ const scopeMessage = scope ? ' to scope ' + scope : ''
77+ const message = `Logged in as ${ username } ${ scopeMessage } on ${ registry } .`
78+
79+ return {
80+ message,
81+ newCreds : { token }
82+ }
83+ }
84+
85+ module . exports = login
0 commit comments