11import { expect } from 'chai' ;
22import * as request from 'request' ;
3+ import { base64url } from 'rfc4648' ;
4+ import { TextEncoder } from 'util' ;
35
46import { User } from './config_types' ;
57import { OpenIDConnectAuth } from './oidc_auth' ;
68
9+ function encode ( value : string ) : string {
10+ return base64url . stringify ( new TextEncoder ( ) . encode ( value ) ) ;
11+ }
12+
13+ function makeJWT ( header : string , payload : object , signature : string ) : string {
14+ return encode ( header ) + '.' + encode ( JSON . stringify ( payload ) ) + '.' + encode ( signature ) ;
15+ }
16+
717describe ( 'OIDCAuth' , ( ) => {
8- const auth = new OpenIDConnectAuth ( ) ;
18+ var auth : OpenIDConnectAuth ;
19+ beforeEach ( ( ) => {
20+ auth = new OpenIDConnectAuth ( ) ;
21+ } ) ;
22+
23+ it ( 'should correctly parse a JWT' , ( ) => {
24+ const jwt = OpenIDConnectAuth . decodeJWT (
25+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.5mhBHqs5_DTLdINd9p5m7ZJ6XD0Xc55kIaCRY5r6HRA' ,
26+ ) ;
27+ expect ( jwt ) . to . not . be . null ;
28+ } ) ;
29+
30+ it ( 'should correctly parse time from token' , ( ) => {
31+ const time = Math . floor ( Date . now ( ) / 1000 ) ;
32+ const token = makeJWT ( '{}' , { exp : time } , 'fake' ) ;
33+ const timeOut = OpenIDConnectAuth . expirationFromToken ( token ) ;
34+
35+ expect ( timeOut ) . to . equal ( time ) ;
36+ } ) ;
37+
938 it ( 'should be true for oidc user' , ( ) => {
1039 const user = {
1140 authProvider : {
@@ -52,11 +81,13 @@ describe('OIDCAuth', () => {
5281 } ) ;
5382
5483 it ( 'authorization should be undefined if client-id missing' , async ( ) => {
84+ const past = 100 ;
85+ const token = makeJWT ( '{}' , { exp : past } , 'fake' ) ;
5586 const user = {
5687 authProvider : {
5788 name : 'oidc' ,
5889 config : {
59- 'id-token' : 'fakeToken' ,
90+ 'id-token' : token ,
6091 'client-secret' : 'clientsecret' ,
6192 'refresh-token' : 'refreshtoken' ,
6293 'idp-issuer-url' : 'https://www.google.com/' ,
@@ -91,11 +122,13 @@ describe('OIDCAuth', () => {
91122 } ) ;
92123
93124 it ( 'authorization should be undefined if refresh-token missing' , async ( ) => {
125+ const past = 100 ;
126+ const token = makeJWT ( '{}' , { exp : past } , 'fake' ) ;
94127 const user = {
95128 authProvider : {
96129 name : 'oidc' ,
97130 config : {
98- 'id-token' : 'fakeToken' ,
131+ 'id-token' : token ,
99132 'client-id' : 'id' ,
100133 'client-secret' : 'clientsecret' ,
101134 'idp-issuer-url' : 'https://www.google.com/' ,
@@ -109,12 +142,35 @@ describe('OIDCAuth', () => {
109142 expect ( opts . headers . Authorization ) . to . be . undefined ;
110143 } ) ;
111144
145+ it ( 'authorization should work if refresh-token missing but token is unexpired' , async ( ) => {
146+ const future = Date . now ( ) / 1000 + 1000000 ;
147+ const token = makeJWT ( '{}' , { exp : future } , 'fake' ) ;
148+ const user = {
149+ authProvider : {
150+ name : 'oidc' ,
151+ config : {
152+ 'id-token' : token ,
153+ 'client-id' : 'id' ,
154+ 'client-secret' : 'clientsecret' ,
155+ 'idp-issuer-url' : 'https://www.google.com/' ,
156+ } ,
157+ } ,
158+ } as User ;
159+
160+ const opts = { } as request . Options ;
161+ opts . headers = [ ] ;
162+ await auth . applyAuthentication ( user , opts ) ;
163+ expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
164+ } ) ;
165+
112166 it ( 'authorization should be undefined if idp-issuer-url missing' , async ( ) => {
167+ const past = 100 ;
168+ const token = makeJWT ( '{}' , { exp : past } , 'fake' ) ;
113169 const user = {
114170 authProvider : {
115171 name : 'oidc' ,
116172 config : {
117- 'id-token' : 'fakeToken' ,
173+ 'id-token' : token ,
118174 'client-id' : 'id' ,
119175 'client-secret' : 'clientsecret' ,
120176 'refresh-token' : 'refreshtoken' ,
0 commit comments