Skip to content

Commit

Permalink
feat: add login endpoint handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-tkachenko committed Nov 8, 2023
1 parent 6f08c0d commit 9e4d419
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ In addition upon every login, logout or token refresh action prxi-openid-connect
- `HOST_URL` - Public facing Host URL
- `PROXY_REQUEST_TIMEOUT` - [optional] timeout for the proxy requests (default value: `30000`, 30s)
- `UPSTREAM_URL` - the upstream host URL (default value: none)
- `HEALTH_PATH` - [optional] health check api PATH (default value: `/_prxi_/health`)
- `LOGOUT_PATH` - [optional] end session/logout PATH (default value: `/_prxi_/logout`)
- `HEALTH_PATH` - [optional] health check api path (default value: `/_prxi_/health`)
- `LOGOIN_PATH` - [optional] end login endpoint path (default value: `/_prxi_/login`)
- `LOGOUT_PATH` - [optional] end session/logout path (default value: `/_prxi_/logout`)
- `LOG_LEVEL` - [optional] log level (default value: `info`)
- `LOG_FILE` - [optional] log file path (default value: `/prxi/logs/prxi-openid-connect.log`)
- `LOG_FILE_SIZE` - [optional] maximum log file size (default value: `10M`)
Expand Down
2 changes: 1 addition & 1 deletion bin/0-init-keycloak.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

source ../.env

KC_CONTAINER_NAME=keycloak
KC_CONTAINER_NAME=prixi-openidc-keycloak
KC_USERNAME=admin
KC_PASSWORD=admin
KC_SERVER_ADDRESS=http://localhost:8080
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3'
services:
keycloak:
container_name: keycloak
container_name: prixi-openidc-keycloak
image: quay.io/keycloak/keycloak:22.0.1
environment:
KEYCLOAK_ADMIN: admin
Expand Down
2 changes: 2 additions & 0 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { errorHandler } from './handlers/ErrorHandler';
import { OpenIDUtils } from './utils/OpenIDUtils';
import { E404Handler } from './handlers/E404Handler';
import { LogoutHandler } from './handlers/LogoutHandler';
import { LoginHandler } from './handlers/LoginHandler';

// Prepare logger

Expand Down Expand Up @@ -44,6 +45,7 @@ export const start = async (): Promise<Prxi> => {
target: config.upstream,
requestHandlers: [
HealthHandler,
new LoginHandler(),
new LogoutHandler(),
CallbackHandler,
new ProxyHandler(),
Expand Down
1 change: 1 addition & 0 deletions src/config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Config {

healthPath: string;
logoutPath: string;
loginPath: string;

hostURL: string;
openid: {
Expand Down
1 change: 1 addition & 0 deletions src/config/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const getConfig = () => {

healthPath: process.env.HEALTH_PATH || '/_prxi_/health',
logoutPath: process.env.LOGOUT_PATH || '/_prxi_/logout',
loginPath: process.env.LOGIN_PATH || '/_prxi_/login',

hostURL: process.env.HOST_URL,
openid: {
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/CallbackHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { sendErrorResponse, sendRedirect, setAuthCookies } from "../utils/Respon
import { OpenIDUtils } from "../utils/OpenIDUtils";
import { parse } from "cookie";
import getLogger from "../Logger";
import { RequestUtils } from "../utils/RequestUtils";

export const CallbackHandler: RequestHandlerConfig = {
isMatching: (method: HttpMethod, path: string) => {
Expand All @@ -16,7 +17,7 @@ export const CallbackHandler: RequestHandlerConfig = {
let tokens = await OpenIDUtils.exchangeCode(req);
let metaToken: string;

const cookies = parse(req.headers.cookie);
const cookies = RequestUtils.getCookies(req);
const originalPath = cookies[getConfig().cookies.names.originalPath] || '/';

// login webhook handler (if any)
Expand Down
33 changes: 33 additions & 0 deletions src/handlers/LoginHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IncomingMessage, ServerResponse } from "http";
import { HttpMethod, ProxyRequest, RequestHandlerConfig } from "prxi";
import { getConfig } from "../config/getConfig";
import { invalidateAuthCookies, sendRedirect } from "../utils/ResponseUtils";
import { OpenIDUtils } from "../utils/OpenIDUtils";
import { Logger } from "pino";
import getLogger from "../Logger";
import { JwtPayload, verify } from "jsonwebtoken";
import { RequestUtils } from "../utils/RequestUtils";

export class LoginHandler implements RequestHandlerConfig {
private logger: Logger;

constructor() {
this.logger = getLogger('LoginHandler')
}

/**
* @inheritdoc
*/
public isMatching(method: HttpMethod, path: string): boolean {
return method === 'GET' && path === getConfig().loginPath;
}

/**
* @inheritdoc
*/
public async handle(req: IncomingMessage, res: ServerResponse, proxyRequest: ProxyRequest): Promise<void> {
this.logger.info('Handle login request');

await sendRedirect(res, OpenIDUtils.getAuthorizationUrl());
}
}
20 changes: 20 additions & 0 deletions test/LoginHandler.suite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { suite, test } from "@testdeck/mocha";
import { BaseSuite } from "./Base.suite";
import { getConfig } from "../src/config/getConfig";
import { strictEqual } from "assert";

@suite()
export class LoginHandlerSuite extends BaseSuite {
@test()
async login() {
const uri = '/api/test?q=str';
await this.withNewPage(getConfig().hostURL + getConfig().loginPath, async (page) => {
await this.loginOnKeycloak(page);

// make sure we can access the resource
await this.navigate(page, getConfig().hostURL + uri);
const json = await this.getJsonFromPage(page);
strictEqual(json.http.originalUrl, uri);
});
}
}

0 comments on commit 9e4d419

Please sign in to comment.