5
5
6
6
import * as sinon from 'sinon'
7
7
import * as vscode from 'vscode'
8
+ import * as jose from 'jose'
8
9
import { LanguageClientAuth , SsoLogin , IamLogin } from '../../auth/auth2'
9
10
import { LanguageClient } from 'vscode-languageclient'
10
11
import {
@@ -40,7 +41,7 @@ const tokenId = 'test-token'
40
41
describe ( 'LanguageClientAuth' , ( ) => {
41
42
let client : sinon . SinonStubbedInstance < LanguageClient >
42
43
let auth : LanguageClientAuth
43
- const encryptionKey = Buffer . from ( 'test-key' )
44
+ const encryptionKey = Buffer . from ( 'test-key' . padEnd ( 32 , '0' ) )
44
45
let useDeviceFlowStub : sinon . SinonStub
45
46
46
47
beforeEach ( ( ) => {
@@ -53,6 +54,14 @@ describe('LanguageClientAuth', () => {
53
54
sinon . restore ( )
54
55
} )
55
56
57
+ async function encrypt < T > ( request : T ) : Promise < string > {
58
+ const payload = new TextEncoder ( ) . encode ( JSON . stringify ( request ) )
59
+ const encrypted = await new jose . CompactEncrypt ( payload )
60
+ . setProtectedHeader ( { alg : 'dir' , enc : 'A256GCM' } )
61
+ . encrypt ( encryptionKey )
62
+ return encrypted
63
+ }
64
+
56
65
describe ( 'getSsoToken' , ( ) => {
57
66
async function testGetSsoToken ( useDeviceFlow : boolean ) {
58
67
const tokenSource = {
@@ -61,6 +70,16 @@ describe('LanguageClientAuth', () => {
61
70
}
62
71
useDeviceFlowStub . returns ( useDeviceFlow ? true : false )
63
72
73
+ client . sendRequest . resolves ( {
74
+ ssoToken : {
75
+ id : 'my-id' ,
76
+ accessToken : 'my-access-token' ,
77
+ } ,
78
+ updateCredentialsParams : {
79
+ data : 'my-data' ,
80
+ } ,
81
+ } satisfies GetSsoTokenResult )
82
+
64
83
await auth . getSsoToken ( tokenSource , true )
65
84
66
85
sinon . assert . calledOnce ( client . sendRequest )
@@ -95,9 +114,30 @@ describe('LanguageClientAuth', () => {
95
114
96
115
sinon . assert . calledOnce ( client . sendRequest )
97
116
const requestParams = client . sendRequest . firstCall . args [ 1 ]
98
- sinon . assert . match ( requestParams . profile , {
99
- name : profileName ,
100
- } )
117
+ sinon . assert . match (
118
+ requestParams . profile ,
119
+ encrypt ( {
120
+ profile : {
121
+ kinds : [ ProfileKind . SsoTokenProfile ] ,
122
+ name : profileName ,
123
+ settings : {
124
+ region : region ,
125
+ sso_session : profileName ,
126
+ aws_access_key_id : '' ,
127
+ aws_secret_access_key : '' ,
128
+ role_arn : '' ,
129
+ } ,
130
+ } ,
131
+ ssoSession : {
132
+ name : profileName ,
133
+ settings : {
134
+ sso_region : region ,
135
+ sso_start_url : startUrl ,
136
+ sso_registration_scopes : [ 'scope1' ] ,
137
+ } ,
138
+ } ,
139
+ } )
140
+ )
101
141
sinon . assert . match ( requestParams . ssoSession . settings , {
102
142
sso_region : region ,
103
143
} )
@@ -116,13 +156,20 @@ describe('LanguageClientAuth', () => {
116
156
name : profileName ,
117
157
kinds : [ ProfileKind . IamCredentialsProfile ] ,
118
158
} )
119
- sinon . assert . match ( requestParams . profile . settings , {
120
- aws_access_key_id : 'myAccessKey' ,
121
- aws_secret_access_key : 'mySecretKey' ,
122
- aws_session_token : 'mySessionToken' ,
123
- role_arn : '' ,
124
- source_profile : '' ,
125
- } )
159
+ sinon . assert . match (
160
+ requestParams . profile . settings ,
161
+ encrypt ( {
162
+ kinds : [ ProfileKind . IamCredentialProcessProfile ] ,
163
+ name : profileName ,
164
+ settings : {
165
+ aws_access_key_id : 'myAccessKey' ,
166
+ aws_secret_access_key : 'mySecretKey' ,
167
+ aws_session_token : 'mySessionToken' ,
168
+ role_arn : '' ,
169
+ source_profile : '' ,
170
+ } ,
171
+ } )
172
+ )
126
173
} )
127
174
} )
128
175
@@ -213,6 +260,21 @@ describe('LanguageClientAuth', () => {
213
260
214
261
describe ( 'getIamCredential' , ( ) => {
215
262
it ( 'sends correct request parameters' , async ( ) => {
263
+ client . sendRequest . resolves ( {
264
+ credential : {
265
+ id : 'my-id' ,
266
+ kinds : [ ] ,
267
+ credentials : {
268
+ accessKeyId : 'my-access-key' ,
269
+ secretAccessKey : 'my-secret-key' ,
270
+ sessionToken : 'my-session-token' ,
271
+ } ,
272
+ } ,
273
+ updateCredentialsParams : {
274
+ data : 'my-data' ,
275
+ } ,
276
+ } satisfies GetIamCredentialResult )
277
+
216
278
await auth . getIamCredential ( profileName , true )
217
279
218
280
sinon . assert . calledOnce ( client . sendRequest )
0 commit comments