Skip to content

Commit a4f9d70

Browse files
authored
Merge pull request #167 from brendandburns/oidc
Implement OIDC Authentication Support
2 parents 4e93cbe + 87d45c1 commit a4f9d70

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/oidc_auth.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Authenticator } from './auth';
2+
import { User } from './config_types';
3+
4+
export class OpenIDConnectAuth implements Authenticator {
5+
public isAuthProvider(user: User): boolean {
6+
if (!user.authProvider) {
7+
return false;
8+
}
9+
return user.authProvider.name === 'oidc';
10+
}
11+
12+
public getToken(user: User): string | null {
13+
if (!user.authProvider.config || !user.authProvider.config['id-token']) {
14+
return null;
15+
}
16+
// TODO: Handle expiration and refresh here...
17+
// TODO: Extract the 'Bearer ' to config.ts?
18+
return `Bearer ${user.authProvider.config['id-token']}`;
19+
}
20+
}

src/oidc_auth_test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { expect } from 'chai';
2+
3+
import { User } from './config_types';
4+
import { OpenIDConnectAuth } from './oidc_auth';
5+
6+
describe('OIDCAuth', () => {
7+
const auth = new OpenIDConnectAuth();
8+
it('should be true for oidc user', () => {
9+
const user = {
10+
authProvider: {
11+
name: 'oidc',
12+
},
13+
} as User;
14+
15+
expect(auth.isAuthProvider(user)).to.equal(true);
16+
});
17+
18+
it('should be false for other user', () => {
19+
const user = {
20+
authProvider: {
21+
name: 'azure',
22+
},
23+
} as User;
24+
25+
expect(auth.isAuthProvider(user)).to.equal(false);
26+
});
27+
28+
it('should be false for null user.authProvider', () => {
29+
const user = {} as User;
30+
31+
expect(auth.isAuthProvider(user)).to.equal(false);
32+
});
33+
34+
it('get a token if present', () => {
35+
const token = 'some token';
36+
const user = {
37+
authProvider: {
38+
name: 'oidc',
39+
config: {
40+
'id-token': token,
41+
},
42+
},
43+
} as User;
44+
45+
expect(auth.getToken(user)).to.equal(`Bearer ${token}`);
46+
});
47+
48+
it('get null if token missing', () => {
49+
const user = {
50+
authProvider: {
51+
name: 'oidc',
52+
config: {
53+
},
54+
},
55+
} as User;
56+
57+
expect(auth.getToken(user)).to.equal(null);
58+
});
59+
});

0 commit comments

Comments
 (0)