commit 9465e5f05a9fc30868f1bca56c47e03ab531e9d2 Author: Pascal Perrenoud Date: Tue May 14 14:37:32 2024 +0200 Import files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9eda1bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +._* +.DS_Store +.idea/ +bun.lockb +package-lock.json +node_modules/ diff --git a/.woodpecker/test.yml b/.woodpecker/test.yml new file mode 100644 index 0000000..82213b2 --- /dev/null +++ b/.woodpecker/test.yml @@ -0,0 +1,22 @@ +when: + - path: + include: [ + 'src/**/*.ts', + 'index.ts' + ] + +steps: + install: + image: oven/bun:alpine + when: + - event: [pull_request, push, manual] + commands: + - bun install + + test: + image: oven/bun:alpine + when: + - event: [pull_request, push, manual] + depends_on: install + commands: + - bun test diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..385b499 --- /dev/null +++ b/index.ts @@ -0,0 +1,2 @@ +export * as JWT from 'jwt' +export * as pwd from 'pwd' diff --git a/package.json b/package.json new file mode 100644 index 0000000..a514fa9 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "crypto-server", + "description": "Various crypto utils that only works on the server side", + "module": "index.ts", + "type": "module", + "files": ["index.ts"], + "scripts": { + "test": "bun test" + }, + "dependencies": { + "jose": "^5.3.0", + "result": "git+git@git.pband.ch:typescript/result.git" + }, + "devDependencies": { + "@types/bun": "^1.1.2" + }, + "peerDependencies": { + "typescript": "^5.0.0" + } +} diff --git a/src/jwt.ts b/src/jwt.ts new file mode 100644 index 0000000..92a7c36 --- /dev/null +++ b/src/jwt.ts @@ -0,0 +1,10 @@ +import {Result} from 'result' + +export type JWT = `${string}.${string}.${string}` + +export function create() : Result { + throw "todo" +} +export function verify() : boolean { + throw "todo" +} diff --git a/src/pwd.ts b/src/pwd.ts new file mode 100644 index 0000000..1804fc3 --- /dev/null +++ b/src/pwd.ts @@ -0,0 +1,7 @@ +export function hash(pwd: string) : Promise { + return Bun.password.hash(pwd) +} + +export async function verify(pwd: string, hash: string) : Promise { + return Bun.password.verify(pwd, hash).catch(() => false) +} diff --git a/test/pwd.test.ts b/test/pwd.test.ts new file mode 100644 index 0000000..3e58eb1 --- /dev/null +++ b/test/pwd.test.ts @@ -0,0 +1,35 @@ +import {test, expect} from 'bun:test' + +import {pwd} from '../index' + +test('base case', async () => { + const password = "AwesomePassword123!" + const hash = await pwd.hash(password) + const verification = await pwd.verify(password, hash) + expect(verification).toBe(true) +}) + +test('wrong password', async () => { + const password1 = "AwesomePassword123!" + const password2 = "AwesomePassword321!" + expect(password1).not.toEqual(password2) + + const hash = await pwd.hash(password1) + const verification = await pwd.verify(password2, hash) + expect(verification).toBe(false) +}) + +test('salt changes', async () => { + const password = "AwesomePassword123!" + const hash1 = await pwd.hash(password) + const hash2 = await pwd.hash(password) + expect(hash1).not.toEqual(hash2) +}) + +test('tampered hash', async () => { + const password = "AwesomePassword123" + const hash = await pwd.hash(password) + const tamperedHash = hash.replace('a', 'b') + const verification = await pwd.verify(password, tamperedHash) + expect(verification).toBe(false) +}) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..de704e7 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,35 @@ +{ + "compilerOptions": { + "baseUrl": "./src", + + // Enable latest features + "lib": ["ESNext"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "allowJs": true, + "checkJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "forceConsistentCasingInFileNames": true, + "noPropertyAccessFromIndexSignature": false + }, + + "include": [ + "index.ts", + "src/**/*.ts" + ] +}