feat: first somewhat working version

This commit is contained in:
2026-09-10 11:42:05 +02:00
parent 57e063eacb
commit e12694bf0a
13 changed files with 3968 additions and 1 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
# auth # auth
Wrapper on top of Keycloak's Connect library for faster, but less flexible integration Wrapper on top of `openid-client`, with integration into `express.js`
Executable
+14
View File
@@ -0,0 +1,14 @@
#!/bin/bash
tsc --declaration
cp ./package.json ./dist
cp ./README.md ./dist
mkdir dist/public || true
cp ./public/auto-redirect.html ./dist/public/auto-redirect.html
cp ./public/error-page.html ./dist/public/error-page.html
if [[ -z $1 ]]; then
rm dist/testing.js
rm dist/testing.d.ts
fi
echo "Build complete"
+744
View File
@@ -0,0 +1,744 @@
import eslint from '@eslint/js';
import globals from 'globals';
import stylistic from '@stylistic/eslint-plugin';
import tseslint from 'typescript-eslint';
import typescript from '@typescript-eslint/eslint-plugin';
import vue from 'eslint-plugin-vue';
const style = {
'plugins': {
'@stylistic': stylistic,
'@stylistic/js': stylistic,
'@stylistic/ts': stylistic
},
'files': [
'**/*.ts',
'**/*.js',
'**/*.mjs',
'**/*.cjs',
'**/*.tsx',
'**/*.jsx'
],
'rules': {
'sort-imports': [
'warn',
{
'ignoreCase': false,
'ignoreDeclarationSort': false,
'ignoreMemberSort': false,
'memberSyntaxSortOrder': [
'none',
'all',
'multiple',
'single'
],
'allowSeparatedGroups': false
}
],
// Formatting
'@stylistic/array-bracket-newline': [
'error',
{
'multiline': false,
'minItems': 2
}
],
'@stylistic/array-bracket-spacing': [
'error',
'always'
],
'@stylistic/array-element-newline': [
'error',
{
'consistent': false,
'multiline': false,
'minItems': 2
}
],
'@stylistic/arrow-parens': [
'error',
'as-needed'
],
'@stylistic/arrow-spacing': [
'error',
{
'before': true,
'after': true
}
],
'@stylistic/block-spacing': [
'error',
'always'
],
'@stylistic/brace-style': [
'error',
'1tbs',
{
'allowSingleLine': false
}
],
'@stylistic/comma-dangle': [
'error',
'never'
],
'@stylistic/comma-spacing': [
'error',
{
'before': false,
'after': true
}
],
'@stylistic/comma-style': [
'error',
'last'
],
'@stylistic/dot-location': [
'error',
'property'
],
'@stylistic/function-call-argument-newline': [
'error',
'consistent'
],
'@stylistic/function-call-spacing': [
'error',
'never'
],
'@stylistic/function-paren-newline': [
'error',
'multiline-arguments'
],
'@stylistic/implicit-arrow-linebreak': [
'error',
'beside'
],
'@stylistic/indent': [
'error',
4
],
'@stylistic/indent-binary-ops': [
'error',
4
],
'@stylistic/key-spacing': [
'error',
{
'beforeColon': false,
'afterColon': true
}
],
'@stylistic/keyword-spacing': [
'error',
{
'before': true,
'after': true
}
],
'@stylistic/lines-between-class-members': [
'error',
'always'
],
'@stylistic/max-len': [
'warn',
{
'code': 140,
'comments': 160,
'ignoreComments': false,
'ignoreUrls': true,
'ignoreStrings': true,
'ignoreTemplateLiterals': true,
'ignoreRegExpLiterals': true
}
],
'@stylistic/max-statements-per-line': [
'error',
{
'max': 1
}
],
'@stylistic/multiline-ternary': [
'error',
'always-multiline'
],
'@stylistic/new-parens': [
'error',
'always'
],
'@stylistic/newline-per-chained-call': 'error',
'@stylistic/no-confusing-arrow': 'error',
'@stylistic/no-extra-parens': [
'error',
'all',
{
'nestedBinaryExpressions': false,
'ternaryOperandBinaryExpressions': false,
'ignoreJSX': 'multi-line',
'nestedConditionalExpressions': false
}
],
'@stylistic/no-extra-semi': 'error',
'@stylistic/no-floating-decimal': 'error',
'@stylistic/no-mixed-operators': 'error',
'@stylistic/no-mixed-spaces-and-tabs': 'error',
'@stylistic/no-multi-spaces': 'error',
'@stylistic/no-multiple-empty-lines': [
'error',
{
'max': 3,
'maxEOF': 2
}
],
'@stylistic/no-tabs': 'error',
'@stylistic/no-trailing-spaces': 'error',
'@stylistic/no-whitespace-before-property': 'error',
'@stylistic/object-curly-newline': [
'error',
{
'multiline': true,
'minProperties': 1
}
],
'@stylistic/object-curly-spacing': [
'error',
'always'
],
'@stylistic/object-property-newline': 'error',
'@stylistic/one-var-declaration-per-line': 'error',
'@stylistic/operator-linebreak': [
'error',
'before'
],
'@stylistic/padded-blocks': [
'error',
{
'blocks': 'never',
'classes': 'always',
'switches': 'never'
}
],
// Padding lines. The most in-depth part of this config
'@stylistic/padding-line-between-statements': [
'error',
// Variables, Constants
{
'blankLine': 'never',
'prev': 'var',
'next': 'var'
},
{
'blankLine': 'never',
'prev': 'let',
'next': 'let'
},
{
'blankLine': 'never',
'prev': 'const',
'next': 'const'
},
{
'blankLine': 'always',
'prev': 'var',
'next': [
'block',
'block-like',
'break',
'cjs-export',
'cjs-import',
'class',
'const',
'continue',
'debugger',
'directive',
'do',
'empty',
'export',
'expression',
'for',
'function',
'if',
'iife',
'import',
'let',
'return',
'switch',
'throw',
'try',
'var',
'with'
]
},
{
'blankLine': 'always',
'prev': 'let',
'next': [
'block',
'block-like',
'break',
'cjs-export',
'cjs-import',
'class',
'const',
'continue',
'debugger',
'directive',
'do',
'empty',
'export',
'expression',
'for',
'function',
'if',
'iife',
'import',
'return',
'switch',
'throw',
'try',
'var',
'while',
'with'
]
},
{
'blankLine': 'always',
'prev': 'const',
'next': [
'block',
'block-like',
'break',
'cjs-export',
'cjs-import',
'class',
'continue',
'debugger',
'directive',
'do',
'empty',
'export',
'expression',
'for',
'function',
'if',
'iife',
'import',
'let',
'return',
'switch',
'throw',
'try',
'var',
'while',
'with'
]
},
// Import
{
'blankLine': 'never',
'prev': 'import',
'next': 'import'
},
{
'blankLine': 'never',
'prev': 'cjs-import',
'next': 'cjs-import'
},
{
'blankLine': 'always',
'prev': [
'block',
'block-like',
'break',
'cjs-export',
'class',
'const',
'continue',
'debugger',
'directive',
'do',
'empty',
'export',
'expression',
'for',
'function',
'if',
'iife',
'let',
'return',
'switch',
'throw',
'try',
'var',
'while',
'with'
],
'next': 'cjs-import'
},
{
'blankLine': 'always',
'prev': 'cjs-import',
'next': [
'block',
'block-like',
'break',
'cjs-export',
'class',
'const',
'continue',
'debugger',
'directive',
'do',
'empty',
'export',
'expression',
'for',
'function',
'if',
'iife',
'let',
'return',
'switch',
'throw',
'try',
'var',
'while',
'with'
]
},
{
'blankLine': 'always',
'prev': [
'block',
'block-like',
'break',
'cjs-export',
'class',
'const',
'continue',
'debugger',
'directive',
'do',
'empty',
'export',
'expression',
'for',
'function',
'if',
'iife',
'let',
'return',
'switch',
'throw',
'try',
'var',
'while',
'with'
],
'next': 'import'
},
{
'blankLine': 'always',
'prev': 'import',
'next': [
'block',
'block-like',
'break',
'cjs-export',
'class',
'const',
'continue',
'debugger',
'directive',
'do',
'empty',
'export',
'expression',
'for',
'function',
'if',
'iife',
'let',
'return',
'switch',
'throw',
'try',
'var',
'while',
'with'
]
},
// If
{
'blankLine': 'always',
'prev': '*',
'next': 'if'
},
{
'blankLine': 'always',
'prev': 'if',
'next': '*'
},
// For
{
'blankLine': 'always',
'prev': '*',
'next': 'for'
},
{
'blankLine': 'always',
'prev': 'for',
'next': '*'
},
// While
{
'blankLine': 'always',
'prev': '*',
'next': 'while'
},
{
'blankLine': 'always',
'prev': 'while',
'next': '*'
},
// Functions
{
'blankLine': 'always',
'prev': '*',
'next': 'function'
},
{
'blankLine': 'always',
'prev': 'function',
'next': '*'
},
// Block Statements
{
'blankLine': 'always',
'prev': '*',
'next': 'block-like'
},
{
'blankLine': 'always',
'prev': 'block-like',
'next': '*'
},
// Switch
{
'blankLine': 'always',
'prev': '*',
'next': 'switch'
},
{
'blankLine': 'always',
'prev': 'switch',
'next': '*'
},
// Try-Catch
{
'blankLine': 'always',
'prev': '*',
'next': 'try'
},
{
'blankLine': 'always',
'prev': 'try',
'next': '*'
},
// Throw
{
'blankLine': 'always',
'prev': '*',
'next': 'throw'
},
{
'blankLine': 'always',
'prev': 'throw',
'next': '*'
},
// Return
{
'blankLine': 'never',
'prev': 'return',
'next': '*'
},
{
'blankLine': 'always',
'prev': '*',
'next': 'return'
},
// Export
{
'blankLine': 'always',
'prev': '*',
'next': 'export'
},
{
'blankLine': 'always',
'prev': 'export',
'next': '*'
},
{
'blankLine': 'always',
'prev': '*',
'next': 'cjs-export'
},
{
'blankLine': 'always',
'prev': 'cjs-export',
'next': '*'
},
// Classes
{
'blankLine': 'always',
'prev': '*',
'next': 'class'
},
{
'blankLine': 'always',
'prev': 'class',
'next': '*'
}
],
'@stylistic/quote-props': [
'error',
'always'
],
'@stylistic/quotes': [
'error',
'single'
],
'@stylistic/rest-spread-spacing': [
'error',
'never'
],
'@stylistic/semi': [
'error',
'always'
],
'@stylistic/semi-spacing': [
'error',
{
'before': false,
'after': true
}
],
'@stylistic/semi-style': [
'error',
'last'
],
'@stylistic/space-before-blocks': [
'error',
'always'
],
'@stylistic/space-before-function-paren': [
'error',
'always'
],
'@stylistic/space-in-parens': [
'error',
'always'
],
'@stylistic/space-infix-ops': [
'error',
{
'int32Hint': false
}
],
'@stylistic/space-unary-ops': 'error',
'@stylistic/spaced-comment': [
'error',
'always'
],
'@stylistic/switch-colon-spacing': 'error',
'@stylistic/template-curly-spacing': [
'error',
'always'
],
'@stylistic/template-tag-spacing': [
'error',
'always'
],
'@stylistic/type-generic-spacing': 'error',
'@stylistic/type-named-tuple-spacing': 'error',
'@stylistic/wrap-iife': [
'error',
'inside'
],
'@stylistic/wrap-regex': 'error',
'@stylistic/ts/type-annotation-spacing': 'error'
}
};
/** @type {import('eslint').Linter.Config} */
export default tseslint.config(
// Base JavaScript rules
eslint.configs.recommended,
tseslint.configs.recommended,
style,
// Vue support (including TS and JSX inside SFCs)
{
'files': [ '**/*.vue' ],
'languageOptions': {
'sourceType': 'module',
'ecmaVersion': 'latest',
'globals': globals.browser,
'parserOptions': {
'parser': tseslint.parser
}
},
'plugins': {
'vue': vue,
'@stylistic': stylistic,
'@stylistic/js': stylistic,
'@stylistic/ts': stylistic,
'@typescript-eslint': typescript
},
'extends': [
eslint.configs.recommended,
...vue.configs['flat/recommended']
],
'rules': {
...typescript.configs.recommended.rules,
...style.rules,
// Vue specific rules
'@stylistic/indent': 'off',
'vue/html-indent': [
'error',
4
],
'vue/html-comment-indent': [
'error',
4
],
'vue/script-indent': [
'error',
4,
{
'baseIndent': 1,
'switchCase': 1
}
],
'vue/html-self-closing': [
'error',
{
'html': {
'void': 'never',
'normal': 'never',
'component': 'always'
},
'svg': 'always',
'math': 'never'
}
],
'vue/max-attributes-per-line': [
'error',
{
'singleline': 3,
'multiline': 1
}
]
}
}
);
+2878
View File
File diff suppressed because it is too large Load Diff
+42
View File
@@ -0,0 +1,42 @@
{
"name": "@janishutz/oidc-login-sdk",
"version": "1.0.0",
"description": "Simple, limited wrapper on top of passport for oidc authentication, used by my FOSS projects",
"keywords": [
"oidc",
"auth"
],
"repository": {
"type": "git",
"url": "https://git.janishutz.com/janishutz-account/auth"
},
"license": "GPL-3.0-or-later",
"author": "Janis Hutz",
"type": "module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"express": "^5.2.1",
"express-session": "^1.19.0",
"openid-client": "^6.8.8",
"passport": "^0.7.0",
"passport-openidconnect": "^0.1.2"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"@stylistic/eslint-plugin": "^5.10.0",
"@types/express": "^5.0.6",
"@types/express-session": "^1.19.0",
"@types/node": "^22.20.2",
"@types/openid-client": "^3.1.6",
"@types/passport": "^1.0.17",
"@types/passport-openidconnect": "^0.1.3",
"eslint": "^10.10.0",
"eslint-plugin-vue": "^10.11.0",
"globals": "^17.12.0",
"typescript": "^6.0.3",
"typescript-eslint": "^8.70.0"
}
}
+35
View File
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login - janishutz.com Account SDK</title>
<style>
html, body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
color: white;
background-color: #202020;
font-size: 200%;
}
</style>
</head>
<body>
<h1>Redirecting to login</h1>
<p>Please be patient...</p>
<script>
location.href = '[[ redirect ]]'
</script>
</body>
</html>
+35
View File
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login - janishutz.com Account SDK</title>
<style>
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
color: white;
background-color: #202020;
font-size: 200%;
}
</style>
</head>
<body>
<h1>Failed to log in</h1>
<p>Click <a href="/auth/v2/login">here</a> to try again</p>
</body>
</html>
+15
View File
@@ -0,0 +1,15 @@
import express from 'express';
export const ensureAuth = ( redirectOnError?: string ) => {
return ( request: express.Request, response: express.Response, next: express.NextFunction ) => {
if ( request.isAuthenticated() ) {
next();
} else {
if ( redirectOnError ) {
response.redirect( redirectOnError );
} else {
response.sendStatus( 401 );
}
}
};
};
+36
View File
@@ -0,0 +1,36 @@
declare global {
namespace Express {
interface User {
'id': string;
'username'?: string;
'displayName'?: string;
'emails'?: {
'value': string,
'type'?: string
}[];
}
}
}
export interface OIDCConfig {
'clientID': string;
'clientSecret': string;
'issuer': URL;
'scopes'?: ( 'email' | 'profile' )[],
}
export interface AppConfig {
'failRedirect'?: string;
'successRedirect'?: string;
'logoutRedirect'?: string;
'url': URL;
'sessionSecret': string;
'cookieName'?: string;
}
export interface Config {
'oidc': OIDCConfig;
'prod': boolean;
'app': AppConfig
}
+12
View File
@@ -0,0 +1,12 @@
import express from 'express';
export * from './routes.js';
export * from './auth.js';
export type * from './dtype.d.ts';
export const getUserId = ( request: express.Request ): string | undefined => {
return request.user?.id;
};
+103
View File
@@ -0,0 +1,103 @@
import {
Config
} from './dtype.js';
import OIDCStrategy from 'passport-openidconnect';
import {
discovery
} from 'openid-client';
import {
ensureAuth
} from './auth.js';
import express from 'express';
import passport from 'passport';
import session from 'express-session';
/**
* Add the necessary routes for login
* @param app - Express application to register routes to
* @param config - Configuration for the Auth system
* @param verify - A verification function. Should create user if user doesn't exist
* @param getUser - Function to get a user by user ID. Optional, if not provided, only UID will be recovered (should be good enough in most cases)
*/
export const addRoutes = async (
app: express.Application,
config: Config,
verify: ( profile: OIDCStrategy.Profile ) => Promise<boolean>,
getUser?: ( id: string ) => Promise<Express.User>
) => {
app.use( session( {
'secret': config.app.sessionSecret,
'cookie': {
'httpOnly': true,
'secure': config.prod
},
'resave': false,
'saveUninitialized': false
} ) );
app.use( passport.session() );
app.use( passport.initialize() );
passport.serializeUser( ( user, cb ) => {
cb( null, user.id );
} );
if ( getUser )
passport.deserializeUser<string>( async ( user, cb ) => {
return cb( null, await getUser( user ) );
} );
else
passport.deserializeUser<string>( ( user, cb ) => {
return cb( null, {
'id': user
} );
} );
const oidcServerConfig = ( await discovery( config.oidc.issuer, config.oidc.clientID, config.oidc.clientSecret ) ).serverMetadata();
const iss = config.oidc.issuer.toString();
passport.use( new OIDCStrategy(
{
'clientID': config.oidc.clientID,
'clientSecret': config.oidc.clientSecret,
'callbackURL': config.app.url.toString() + 'auth/v2/verify',
'authorizationURL': oidcServerConfig.authorization_endpoint ?? oidcServerConfig.issuer + '/auth',
'issuer': iss,
'userInfoURL': oidcServerConfig.userinfo_endpoint ?? oidcServerConfig.issuer + '/me',
'tokenURL': oidcServerConfig.token_endpoint ?? oidcServerConfig.issuer + '/token',
'scope': config.oidc.scopes
},
async ( issuer: string, profile: OIDCStrategy.Profile, cb: OIDCStrategy.VerifyCallback ) => {
if ( issuer !== iss ) cb( new Error( 'ERR_FORBIDDEN' ) );
cb( null, profile, await verify( profile ) );
}
) );
// TODO: Allow CORS here
app.get( '/auth/v2/login', passport.authenticate( 'openidconnect' ) );
app.get( '/auth/v2/fail', ( _request, response ) => {
response.sendFile( './public/error-page.html' );
} );
app.get( '/auth/v2/verify', passport.authenticate( 'openidconnect', {
'failureRedirect': config.app.failRedirect ?? '/auth/v2/fail',
'failureMessage': true
} ), ( request: express.Request, response: express.Response ) => {
request.session.save();
response.redirect( config.app.successRedirect ?? '/' );
} );
// TODO: Allow CORS here
app.get( '/auth/v2/logout', ensureAuth( config.app.failRedirect ), ( request: express.Request, response: express.Response ) => {
request.logout( {
'keepSessionInfo': false
}, err => {
console.error( err );
} );
// Ensure session is fully destroyed
request.session.destroy( () => {} );
response.redirect( config.app.logoutRedirect ?? '/' );
} );
};
+35
View File
@@ -0,0 +1,35 @@
import {
addRoutes,
ensureAuth
} from './index.js';
import express from 'express';
const app = express();
addRoutes( app, {
'oidc': {
'clientID': process.env.CLIENT_ID ?? '',
'clientSecret': process.env.CLIENT_SECRET ?? '',
'issuer': new URL( 'https://home.janishutz.com/oidc' )
},
'app': {
'sessionSecret': 'secret',
'url': new URL( 'https://home2.janishutz.com' ),
'successRedirect': '/account'
},
'prod': false
}, async uid => {
console.log( uid );
return true;
} );
app.get( '/', ( _request, response ) => {
response.send( 'Hello World' );
} );
app.get( '/account', ensureAuth( '/' ), ( _request, response ) => {
response.send( 'Account' );
} );
app.listen( 8080 );
+18
View File
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"allowJs": true,
"target": "ES2022",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"types": [
"node"
],
"module": "NodeNext",
"moduleResolution": "NodeNext"
},
"include": [
"./src/**/*"
]
}