fix unreliable 'successful' flag, don't set content-type on leave_open
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Hocker 2022-01-04 13:39:00 +01:00
parent dab45e39a6
commit d28be9e3f8
Signed by: Timo
GPG Key ID: DFAC2CF4E1D1BEC9
3 changed files with 32 additions and 4 deletions

View File

@ -86,7 +86,7 @@ class AuthRequest {
data, data,
leave_open leave_open
}: AccessSettings): Promise<AccessResult> { }: AccessSettings): Promise<AccessResult> {
this.default_header (typeof redirect_to !== 'string'); this.default_header (typeof redirect_to !== 'string' && !leave_open);
const at = await auth.sign ( const at = await auth.sign (
'access_token', 'access_token',
@ -122,6 +122,8 @@ class AuthRequest {
result.refresh_token_id = rt.id; result.refresh_token_id = rt.id;
} }
this._is_successful = true;
if (typeof redirect_to === 'string') { if (typeof redirect_to === 'string') {
this.response.setHeader ('Location', redirect_to); this.response.setHeader ('Location', redirect_to);
this.response.statusCode = 302; this.response.statusCode = 302;
@ -135,7 +137,6 @@ class AuthRequest {
this.response.end (JSON.stringify (res)); this.response.end (JSON.stringify (res));
} }
this._is_successful = true;
return result; return result;
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@sapphirecode/auth-server-helper", "name": "@sapphirecode/auth-server-helper",
"version": "2.1.2", "version": "2.1.3",
"main": "dist/index.js", "main": "dist/index.js",
"author": { "author": {
"name": "Timo Hocker", "name": "Timo Hocker",

View File

@ -5,6 +5,7 @@
* Created by Timo Hocker <timo@scode.ovh>, January 2021 * Created by Timo Hocker <timo@scode.ovh>, January 2021
*/ */
/* eslint-disable max-lines */
import http, { IncomingMessage, ServerResponse } from 'http'; import http, { IncomingMessage, ServerResponse } from 'http';
import { to_b64 } from '@sapphirecode/encoding-helper'; import { to_b64 } from '@sapphirecode/encoding-helper';
import auth from '../../lib/Authority'; import auth from '../../lib/Authority';
@ -55,7 +56,8 @@ describe ('auth handler', () => {
beforeAll (() => { beforeAll (() => {
clock_setup (); clock_setup ();
const ah = create_auth_handler ((req) => { // eslint-disable-next-line complexity, max-lines-per-function
const ah = create_auth_handler (async (req) => {
if (!req.is_basic && !req.is_bearer) { if (!req.is_basic && !req.is_bearer) {
let body_auth = false; let body_auth = false;
try { try {
@ -94,6 +96,14 @@ describe ('auth handler', () => {
redirect_to: '/redirected' redirect_to: '/redirected'
}); });
} }
else if (req.user === 'leave' && req.password === 'open') {
req.response.setHeader ('Content-Type', 'text/plain');
await req.allow_access ({
access_token_expires_in: expires_seconds,
leave_open: true
});
req.response.end ('custom response');
}
else { else {
req.deny (); req.deny ();
} }
@ -320,4 +330,21 @@ describe ('auth handler', () => {
error_description: 'unknown authorization type' error_description: 'unknown authorization type'
}); });
}); });
it ('should not set content-type when leave-open is specified', async () => {
const resp1 = await get ({ authorization: 'Basic leave:open' });
expect (resp1.statusCode)
.toEqual (200);
expect (resp1.headers['content-type'])
.toEqual ('text/plain');
expect (resp1.body)
.toEqual ('custom response');
let signature = '';
for (const c of resp1.headers['set-cookie'] as string[]) {
if (c.includes ('cookie_jar='))
signature = c.replace ('cookie_jar=', '');
}
expect (signature).not.toEqual ('');
check_token (signature, 'access_token');
});
}); });