@@ -4,49 +4,52 @@ const _ = require('lodash');
4
4
const CFError = require ( 'cf-errors' ) ;
5
5
const DEFAULTS = require ( '../../defaults' ) ;
6
6
const { auth } = require ( '../../../../logic' ) ;
7
- const { JWTContext } = auth . contexts ;
7
+ const { JWTContext, APIKeyContext } = auth . contexts ;
8
8
const authManager = auth . manager ;
9
9
const authRoot = require ( '../root/auth.cmd' ) ;
10
10
11
11
const _loginWithToken = async ( url , token ) => {
12
+ let authContext ;
12
13
try {
13
- const authContext = JWTContext . createFromToken ( token , url ) ;
14
+ authContext = JWTContext . createFromToken ( token , url ) ;
14
15
return authContext ;
15
16
16
17
} catch ( err ) {
17
- const error = new CFError ( {
18
- cause : err ,
19
- message : 'Failed to login with token' ,
20
- } ) ;
21
- throw error ;
18
+ try {
19
+ authContext = APIKeyContext . createFromToken ( token , url ) ;
20
+ return authContext ;
21
+
22
+ } catch ( err ) {
23
+ const error = new CFError ( {
24
+ cause : err ,
25
+ message : 'Failed to login with api key' ,
26
+ } ) ;
27
+ throw error ;
28
+ }
22
29
}
23
30
} ;
24
31
25
32
const command = new Command ( {
26
- command : 'create-context < name> ' ,
33
+ command : 'create-context [ name] ' ,
27
34
description : 'create-context' ,
28
35
builder : ( yargs ) => {
29
36
return yargs
30
- . usage ( 'Create a new context from a given token ' )
37
+ . usage ( 'Create a new authentication context using an API key ' )
31
38
. option ( 'url' , {
32
39
describe : 'Codefresh system custom url' ,
33
40
default : DEFAULTS . URL ,
34
41
} )
35
42
. positional ( 'name' , {
36
43
describe : 'Context name' ,
44
+ default : 'default' ,
37
45
} )
38
- . option ( 'token ' , {
39
- describe : 'Access token ' ,
46
+ . option ( 'api-key ' , {
47
+ describe : 'API key ' ,
40
48
required : true ,
41
- } )
42
- . option ( 'type' , {
43
- describe : 'Token type' ,
44
- choices : [ JWTContext . TYPE ] ,
45
- default : JWTContext . TYPE ,
46
49
} ) ;
47
50
} ,
48
51
handler : async ( argv ) => {
49
- const authContext = await _loginWithToken ( argv . url , argv . token ) ;
52
+ const authContext = await _loginWithToken ( argv . url , argv [ 'api-key' ] ) ;
50
53
51
54
await authContext . validate ( ) ;
52
55
0 commit comments