This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fbc575
commit 49fba95
Showing
8 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ServerResponse, IncomingMessage } from 'http'; | ||
import { $log } from 'ts-log-debug'; | ||
import { extractAccessToken } from '../utils/RequestUtil'; | ||
import { sendError } from '../utils/ResponseUtil'; | ||
import { sendJSONResponse } from '../utils/ResponseUtil'; | ||
import { JWT } from '../models/JWT'; | ||
|
||
const handler = async (req: IncomingMessage, res: ServerResponse) => { | ||
$log.debug('Handling logout request'); | ||
const accessToken = extractAccessToken(req); | ||
|
||
if (!accessToken) { | ||
await sendError(res, 401, 'Unauthorized'); | ||
|
||
return; | ||
} | ||
|
||
const jwtToken = new JWT(accessToken); | ||
|
||
await sendJSONResponse(res, jwtToken.getAllRoles()); | ||
}; | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { suite, test } from 'mocha-typescript'; | ||
import { get } from 'config'; | ||
import axios from 'axios'; | ||
import { strictEqual, ok } from 'assert'; | ||
import { start, stop } from '../../../src'; | ||
import { IClientConfiguration } from '../../../src/interfaces/IClientConfiguration'; | ||
import { stringify } from 'querystring'; | ||
|
||
const clients: IClientConfiguration[] = get('keycloak.clients'); | ||
|
||
@suite() | ||
class RolesRoute { | ||
async before() { | ||
await start(); | ||
} | ||
|
||
async after() { | ||
await stop(); | ||
} | ||
|
||
@test() | ||
async makeRolesRequest() { | ||
const request = axios.create({ | ||
baseURL: get('host'), | ||
}); | ||
|
||
const accessToken = await this.getAccessToken(); | ||
|
||
const response = await request.get(get('paths.roles'), { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
|
||
ok(response.data.indexOf('test:uma_protection') >= 0); | ||
ok(response.data.indexOf('offline_access') >= 0); | ||
} | ||
|
||
@test() | ||
async makeUnauthorizedRolesRequest() { | ||
const request = axios.create({ | ||
baseURL: get('host'), | ||
}); | ||
|
||
let code = 0; | ||
try { | ||
await request.get(get('paths.roles')); | ||
} catch (e) { | ||
code = e.response.status; | ||
} | ||
|
||
strictEqual(code, 401); | ||
} | ||
|
||
/** | ||
* Authenticate with service account | ||
*/ | ||
private async getAccessToken(): Promise<string> { | ||
console.log('-> Retrieving access_token from KC...'); | ||
const request = axios.create({ | ||
baseURL: clients[0].realmURL.private, | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}); | ||
|
||
const result = await request('/protocol/openid-connect/token', { | ||
method: 'POST', | ||
data: stringify({ | ||
grant_type: 'client_credentials', | ||
client_id: 'test', | ||
client_secret: clients[0].secret, | ||
}), | ||
}); | ||
|
||
console.log(`-> Access token: ${result.data.access_token}`); | ||
|
||
return result.data.access_token; | ||
} | ||
} |