1
- const debug = require ( 'debug' ) ( 'codefresh:auth:login' ) ;
2
1
const Command = require ( '../../Command' ) ;
3
- const _ = require ( 'lodash' ) ;
4
- const CFError = require ( 'cf-errors' ) ;
5
2
const DEFAULTS = require ( '../../defaults' ) ;
6
- const { auth } = require ( '../../../../logic' ) ;
7
- const { JWTContext, APIKeyContext } = auth . contexts ;
8
- const authManager = auth . manager ;
3
+ const { Config } = require ( 'codefresh-sdk' ) ;
9
4
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
- } ;
32
5
33
6
const command = new Command ( {
34
7
command : 'create-context [name]' ,
@@ -40,7 +13,7 @@ const command = new Command({
40
13
title : 'Create Context' ,
41
14
weight : 20 ,
42
15
} ,
43
- builder : ( yargs ) => {
16
+ builder : ( yargs ) => { // eslint-disable-line
44
17
return yargs
45
18
. option ( 'url' , {
46
19
describe : 'Codefresh system custom url' ,
@@ -56,38 +29,25 @@ const command = new Command({
56
29
} )
57
30
. example ( 'codefresh auth create-context --api-key KEY' , 'Creating a default context using KEY as the api-key' )
58
31
. example ( 'codefresh auth create-context my-context --api-key KEY' , 'Creating a named context' ) ;
59
-
60
32
} ,
61
33
handler : async ( argv ) => {
62
- const authContext = await _loginWithToken ( argv . url , argv [ 'api-key' ] ) ;
63
- const userData = await authContext . validate ( ) ;
64
- const roles = _ . get ( userData , 'roles' ) ;
34
+ const configManager = Config . manager ( ) ;
35
+ await configManager . loadConfig ( { configFilePath : argv . cfconfig } ) ;
65
36
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
- }
37
+ const updatedExistingContext = configManager . getContextByName ( argv . name ) ;
78
38
79
- await authManager . addContext ( authContext ) ;
80
- await authManager . setCurrentContext ( authContext ) ;
81
- await authManager . persistContexts ( authContext ) ;
82
- await createContext ( ) ;
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 ( ) ;
83
43
84
44
if ( updatedExistingContext ) {
85
- console . log ( `Updated context: ${ authContext . name } ` ) ;
45
+ console . log ( `Updated context: ${ context . name } ` ) ;
86
46
} else {
87
- console . log ( `Created new context: ${ authContext . name } ` ) ;
47
+ console . log ( `Created new context: ${ context . name } ` ) ;
88
48
}
89
49
90
- console . log ( `Switched to context: ${ authContext . name } ` ) ;
50
+ console . log ( `Switched to context: ${ context . name } ` ) ;
91
51
} ,
92
52
} ) ;
93
53
0 commit comments