1
+ const debug = require ( 'debug' ) ( 'codefresh:auth:login' ) ;
1
2
const Command = require ( '../../Command' ) ;
3
+ const _ = require ( 'lodash' ) ;
4
+ const CFError = require ( 'cf-errors' ) ;
2
5
const DEFAULTS = require ( '../../defaults' ) ;
3
- const { Config } = require ( 'codefresh-sdk' ) ;
6
+ const { auth } = require ( '../../../../logic' ) ;
7
+ const { JWTContext, APIKeyContext } = auth . contexts ;
8
+ const authManager = auth . manager ;
4
9
const authRoot = require ( '../root/auth.cmd' ) ;
10
+ const { createContext } = require ( '../../../../logic/api/auth' ) ;
11
+
12
+ const _loginWithToken = async ( url , token ) => {
13
+ let authContext ;
14
+ try {
15
+ authContext = JWTContext . createFromToken ( token , url ) ;
16
+ return authContext ;
17
+
18
+ } catch ( err ) {
19
+ try {
20
+ authContext = APIKeyContext . createFromToken ( token , url ) ;
21
+ return authContext ;
22
+
23
+ } catch ( err ) {
24
+ const error = new CFError ( {
25
+ cause : err ,
26
+ message : 'Failed to login with api key' ,
27
+ } ) ;
28
+ throw error ;
29
+ }
30
+ }
31
+ } ;
5
32
6
33
const command = new Command ( {
7
34
command : 'create-context [name]' ,
@@ -13,7 +40,7 @@ const command = new Command({
13
40
title : 'Create Context' ,
14
41
weight : 20 ,
15
42
} ,
16
- builder : ( yargs ) => { // eslint-disable-line
43
+ builder : ( yargs ) => {
17
44
return yargs
18
45
. option ( 'url' , {
19
46
describe : 'Codefresh system custom url' ,
@@ -29,25 +56,38 @@ const command = new Command({
29
56
} )
30
57
. example ( 'codefresh auth create-context --api-key KEY' , 'Creating a default context using KEY as the api-key' )
31
58
. example ( 'codefresh auth create-context my-context --api-key KEY' , 'Creating a named context' ) ;
59
+
32
60
} ,
33
61
handler : async ( argv ) => {
34
- const configManager = Config . manager ( ) ;
35
- await configManager . loadConfig ( { configFilePath : argv . cfconfig } ) ;
62
+ const authContext = await _loginWithToken ( argv . url , argv [ 'api-key' ] ) ;
63
+ const userData = await authContext . validate ( ) ;
64
+ const roles = _ . get ( userData , 'roles' ) ;
36
65
37
- const updatedExistingContext = configManager . getContextByName ( argv . name ) ;
66
+ if ( roles . indexOf ( 'Admin' ) > - 1 ) {
67
+ authContext . onPrem = true ;
68
+ }
69
+
70
+ if ( argv . name ) {
71
+ authContext . setName ( argv . name ) ;
72
+ }
73
+
74
+ let updatedExistingContext = false ;
75
+ if ( authManager . getContextByName ( authContext . getName ( ) ) ) {
76
+ updatedExistingContext = true ;
77
+ }
38
78
39
- const context = await configManager . createContext ( { apiKey : argv . apiKey , url : argv . url , name : argv . name } ) ;
40
- configManager . addContext ( context ) ;
41
- configManager . setCurrentContext ( context ) ;
42
- await configManager . persistConfig ( ) ;
79
+ await authManager . addContext ( authContext ) ;
80
+ await authManager . setCurrentContext ( authContext ) ;
81
+ await authManager . persistContexts ( authContext ) ;
82
+ await createContext ( ) ;
43
83
44
84
if ( updatedExistingContext ) {
45
- console . log ( `Updated context: ${ context . name } ` ) ;
85
+ console . log ( `Updated context: ${ authContext . name } ` ) ;
46
86
} else {
47
- console . log ( `Created new context: ${ context . name } ` ) ;
87
+ console . log ( `Created new context: ${ authContext . name } ` ) ;
48
88
}
49
89
50
- console . log ( `Switched to context: ${ context . name } ` ) ;
90
+ console . log ( `Switched to context: ${ authContext . name } ` ) ;
51
91
} ,
52
92
} ) ;
53
93
0 commit comments