@@ -18,6 +18,7 @@ import { Request } from "express";
18
18
import { WithResourceAccessGuard } from "./resource-access" ;
19
19
import { WithFunctionAccessGuard } from "./function-access" ;
20
20
import { fail } from "assert" ;
21
+ import { SubjectId } from "./subject-id" ;
21
22
22
23
function toDateTime ( date : Date ) : string {
23
24
return date . toISOString ( ) . replace ( "T" , " " ) . replace ( "Z" , "" ) ;
@@ -62,7 +63,7 @@ describe("BearerAuth", () => {
62
63
testUser = await userService . createUser ( {
63
64
identity : {
64
65
authId : "gh-user-1" ,
65
- authName : "user " ,
66
+ authName : "testUser " ,
66
67
authProviderId : "public-github" ,
67
68
} ,
68
69
} ) ;
@@ -110,6 +111,35 @@ describe("BearerAuth", () => {
110
111
await expectError ( async ( ) => bearerAuth . authExpressRequest ( req ) , "cannot find token" ) ;
111
112
} ) ;
112
113
114
+ it ( "tryAuthFromHeaders should successfully authenticate BearerToken (PAT)" , async ( ) => {
115
+ const pat1 = await insertPat ( testUser . id , "pat-1" ) ;
116
+
117
+ const headers = {
118
+ authorization : `Bearer ${ pat1 } ` ,
119
+ } ;
120
+ const subjectId = await bearerAuth . tryAuthFromHeaders ( headers ) ;
121
+
122
+ expect ( subjectId ?. toString ( ) ) . to . equal ( SubjectId . fromUserId ( testUser . id ) . toString ( ) ) ;
123
+ } ) ;
124
+
125
+ it ( "tryAuthFromHeaders should return undefined with missing BearerToken in header" , async ( ) => {
126
+ await insertPat ( testUser . id , "pat-1" ) ;
127
+
128
+ const headers = {
129
+ authorization : `Bearer ` , // missing
130
+ } ;
131
+ expect ( await bearerAuth . tryAuthFromHeaders ( headers ) ) . to . be . undefined ;
132
+ } ) ;
133
+
134
+ it ( "tryAuthFromHeaders should fail to authenticate with missing BearerToken from DB (PAT)" , async ( ) => {
135
+ const patNotStored = "gitpod_pat_GrvGthczSRf3ypqFhNtcRiN5fK6CV7rdCkkPLfpbc_4" ;
136
+
137
+ const headers = {
138
+ authorization : `Bearer ${ patNotStored } ` ,
139
+ } ;
140
+ await expectError ( async ( ) => bearerAuth . tryAuthFromHeaders ( headers ) , "cannot find token" ) ;
141
+ } ) ;
142
+
113
143
async function expectError ( fun : ( ) => Promise < any > , message : string ) {
114
144
try {
115
145
await fun ( ) ;
0 commit comments