Skip to content

Commit 676d936

Browse files
authored
refactor: update dependencies (#9)
* chore: update yarn bundle * refactor(hydra oauth client): update type to module * refactor: add supertest types * style: fix lint * chore: update types cookie package version to 0.6.0 * chore: update types express version to 4.17.21 * chore: update types node package * chore: update types simple oauth2 * chore: update express package version * chore: update get-port package version * chore: update supertest package version * chore: update cookie package version * chore: update simple oauth2 package version * chore: update types react dom package version * chore: update next package version * fix: integration tests * chore: update cookie package version * chore: update express package version * style: fix integration test lint * chore: update types node package version * chore: update supertest package version * chore: change kratos session type to module
1 parent af22bdc commit 676d936

18 files changed

+1430
-2947
lines changed

.config/husky/commit-msg

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
3-
4-
yarn commit message lint
1+
yarn commit message lint

.config/husky/pre-commit

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
3-
4-
yarn commit staged
1+
yarn commit staged

.config/husky/prepare-commit-msg

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
3-
4-
yarn commit message $@
1+
yarn commit message $@

.pnp.cjs

+403-220
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.yarn/releases/yarn-0.0.1-git.20230911.hash-1c44e15.cjs

-1,843
This file was deleted.

.yarn/releases/yarn.cjs

+499-547
Large diffs are not rendered by default.

packages/hydra-oauth-client/integration/test/authorization-code.test.ts

+42-17
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@
22
* @jest-environment node
33
*/
44

5-
import { Express } from 'express'
6-
import { Server } from 'http'
5+
import type { Express } from 'express'
6+
import type { Server } from 'http'
7+
import type { SuperTest } from 'supertest'
8+
import type { Test } from 'supertest'
9+
10+
import { describe } from '@jest/globals'
11+
import { afterAll } from '@jest/globals'
12+
import { beforeAll } from '@jest/globals'
13+
import { expect } from '@jest/globals'
14+
import { it } from '@jest/globals'
715
import express from 'express'
816
import getPort from 'get-port'
917
import supertest from 'supertest'
1018

11-
import { HydraAuthorizationCodeClient } from '../../src'
19+
import { HydraAuthorizationCodeClient } from '../../src/index.js'
1220

1321
describe('authorization code', () => {
1422
let app: Express
1523
let server: Server
16-
let request
24+
let request: SuperTest<Test>
1725

1826
beforeAll(async () => {
1927
const port = await getPort()
@@ -27,12 +35,19 @@ describe('authorization code', () => {
2735
redirectUri: `http://localhost:${port}/callback`,
2836
})
2937

30-
app.use('/login', (req, res) => client.authenticate(req, res))
31-
app.use('/callback', async (req, res) => res.json(await client.verify(req, res)))
38+
app.use('/login', (req, res) => {
39+
client.authenticate(req, res)
40+
})
41+
app.use('/callback', async (req, res): Promise<void> => {
42+
res.json(await client.verify(req, res))
43+
})
3244

33-
app.use('/oauth2/token', (req, res) => res.json({ access_token: true }))
45+
app.use('/oauth2/token', (req, res) => {
46+
res.json({ access_token: true })
47+
})
3448

3549
server = app.listen(port)
50+
// @ts-expect-error
3651
request = supertest.agent(server)
3752
})
3853

@@ -43,7 +58,7 @@ describe('authorization code', () => {
4358
it('authenticate location', async () => {
4459
const response = await request.get('/login')
4560

46-
const location = new URL(response.get('location'))
61+
const location = new URL(response.get('location')!)
4762

4863
expect(location.searchParams.get('client_id')).toBe('client')
4964
expect(location.searchParams.get('response_type')).toBe('code')
@@ -52,23 +67,33 @@ describe('authorization code', () => {
5267
it('authenticate nonce', async () => {
5368
const response = await request.get('/login')
5469

55-
const nonce = response
56-
.get('set-cookie')
57-
.find((item) => item.includes(HydraAuthorizationCodeClient.NONCE_TOKEN))
70+
// @ts-expect-error
71+
const cookies = response.get('set-cookie') as Array<string>
72+
73+
if (!cookies) throw new Error('No cookies')
74+
75+
const nonce = cookies.find((item) => item.includes(HydraAuthorizationCodeClient.NONCE_TOKEN))
5876

5977
expect(nonce).toBeDefined()
6078
})
6179

6280
it('verify', async () => {
6381
const authenticate = await request.get('/login')
6482

65-
const location = new URL(authenticate.get('location'))
83+
const location = new URL(authenticate.get('location')!)
6684

67-
const verify = await request.get('/callback').query({
68-
state: location.searchParams.get('state'),
69-
scope: 'openid offline',
70-
code: 'code',
71-
})
85+
const cookies = authenticate.get('set-cookie')
86+
87+
if (!cookies) throw new Error('No cookies')
88+
89+
const verify = await request
90+
.get('/callback')
91+
.set('Cookie', cookies)
92+
.query({
93+
state: location.searchParams.get('state'),
94+
scope: 'openid offline',
95+
code: 'code',
96+
})
7297

7398
expect(verify.body.accessToken).toBeDefined()
7499
})

packages/hydra-oauth-client/package.json

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@atls/hydra-oauth-client",
33
"version": "0.0.1",
44
"license": "BSD 3-Clause",
5+
"type": "module",
56
"main": "src/index.ts",
67
"files": [
78
"dist"
@@ -12,17 +13,19 @@
1213
"postpack": "rm -rf dist"
1314
},
1415
"dependencies": {
15-
"cookie": "0.5.0",
16-
"simple-oauth2": "5.0.0"
16+
"cookie": "1.0.1",
17+
"simple-oauth2": "5.1.0"
1718
},
1819
"devDependencies": {
19-
"@types/cookie": "0.5.2",
20-
"@types/express": "4.17.17",
21-
"@types/node": "20.6.3",
22-
"@types/simple-oauth2": "5.0.4",
23-
"express": "4.18.2",
24-
"get-port": "7.0.0",
25-
"supertest": "6.3.3"
20+
"@jest/globals": "29.7.0",
21+
"@types/cookie": "0.6.0",
22+
"@types/express": "5.0.0",
23+
"@types/node": "22.7.5",
24+
"@types/simple-oauth2": "5.0.7",
25+
"@types/supertest": "6.0.2",
26+
"express": "5.0.1",
27+
"get-port": "7.1.0",
28+
"supertest": "7.0.0"
2629
},
2730
"publishConfig": {
2831
"access": "public",

packages/hydra-oauth-client/src/hydra-authorization-code.client.ts

+33-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
import { AuthorizationCode } from 'simple-oauth2'
2-
import { ModuleOptions } from 'simple-oauth2'
3-
import { randomBytes } from 'crypto'
4-
import cookie from 'cookie'
5-
6-
import { HydraAuthorizationCodeClientOptions } from './hydra-authorization-code.interfaces'
7-
import { HydraAuthorizationCodeResult } from './hydra-authorization-code.interfaces'
8-
import { AuthenticationStateOptions } from './hydra-authorization-code.interfaces'
9-
import { State } from './hydra-authorization-code.interfaces'
10-
import { serializeState } from './state.utils'
11-
import { parseState } from './state.utils'
1+
import type { Request } from 'express'
2+
import type { Response } from 'express'
3+
import type { ModuleOptions } from 'simple-oauth2'
4+
5+
import type { HydraAuthorizationCodeClientOptions } from './hydra-authorization-code.interfaces.js'
6+
import type { HydraAuthorizationCodeResult } from './hydra-authorization-code.interfaces.js'
7+
import type { AuthenticationStateOptions } from './hydra-authorization-code.interfaces.js'
8+
import type { State } from './hydra-authorization-code.interfaces.js'
9+
10+
import { AuthorizationCode } from 'simple-oauth2'
11+
import { randomBytes } from 'crypto'
12+
import cookie from 'cookie'
13+
14+
import { serializeState } from './state.utils.js'
15+
import { parseState } from './state.utils.js'
1216

1317
export class HydraAuthorizationCodeClient {
1418
static NONCE_TOKEN = 'anonce'
1519

20+
logoutUrl: string
21+
1622
private client: AuthorizationCode
1723

1824
private redirectUri: string
1925

20-
private scope: string[]
21-
22-
logoutUrl: string
26+
private scope: Array<string>
2327

2428
constructor(options: HydraAuthorizationCodeClientOptions) {
2529
const credentials: ModuleOptions = {
@@ -44,11 +48,11 @@ export class HydraAuthorizationCodeClient {
4448
this.logoutUrl = new URL('/oauth2/sessions/logout', options.tokenHost).toString()
4549
}
4650

47-
getReturnToUrl(req): string | undefined {
51+
getReturnToUrl(req: Request): string | undefined {
4852
const query = req.query || req.params
4953

5054
if (query.return_to) {
51-
return query.return_to
55+
return query.return_to as string
5256
}
5357

5458
const referrer = req.get('referrer')
@@ -63,8 +67,8 @@ export class HydraAuthorizationCodeClient {
6367
return undefined
6468
}
6569

66-
setNonce(req, res, nonce: string) {
67-
let setCookieHeader = req.get('Set-Cookie') || []
70+
setNonce(req: Request, res: Response, nonce: string): void {
71+
let setCookieHeader = req.get('Set-Cookie') || ([] as Array<string>)
6872

6973
if (!Array.isArray(setCookieHeader)) {
7074
setCookieHeader = [setCookieHeader]
@@ -81,7 +85,7 @@ export class HydraAuthorizationCodeClient {
8185
res.set('Set-Cookie', setCookieHeader)
8286
}
8387

84-
getAuthorizationUrl(params = {}) {
88+
getAuthorizationUrl(params = {}): string {
8589
const state = serializeState(params)
8690

8791
return this.client.authorizeURL({
@@ -91,29 +95,34 @@ export class HydraAuthorizationCodeClient {
9195
})
9296
}
9397

94-
authenticate(req, res, options: AuthenticationStateOptions = {}) {
98+
authenticate(req: Request, res: Response, options: AuthenticationStateOptions = {}): void {
9599
const params = {
96100
...options,
101+
// eslint-disable-next-line react/no-is-mounted
97102
returnTo: this.getReturnToUrl(req),
98103
nonce: randomBytes(20).toString('hex'),
99104
}
100105

106+
// eslint-disable-next-line react/no-is-mounted
101107
this.setNonce(req, res, params.nonce)
102108

103-
return res.redirect(this.getAuthorizationUrl(params))
109+
// eslint-disable-next-line react/no-is-mounted
110+
res.redirect(this.getAuthorizationUrl(params))
104111
}
105112

106-
async verify(req, res): Promise<HydraAuthorizationCodeResult> {
113+
async verify(req: Request, res: Response): Promise<HydraAuthorizationCodeResult> {
107114
const query = req.query || req.params
108115

109116
const tokenConfig = {
110117
redirect_uri: this.redirectUri,
111-
code: query.code,
112-
scope: query.scope,
118+
code: query.code as string,
119+
scope: query.scope as string,
113120
}
114121

122+
// @ts-expect-error
115123
const state: State = parseState(query.state) || {}
116124

125+
// @ts-expect-error
117126
const cookies = cookie.parse(req.get('cookie'))
118127

119128
if (state.nonce !== cookies[HydraAuthorizationCodeClient.NONCE_TOKEN]) {

packages/hydra-oauth-client/src/hydra-authorization-code.interfaces.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { AccessToken } from 'simple-oauth2'
1+
import type { AccessToken } from 'simple-oauth2'
22

3-
export type StateTargetType = 'login' | 'registration' | 'verification' | 'recovery'
3+
export type StateTargetType = 'login' | 'recovery' | 'registration' | 'verification'
44

55
export interface State {
66
nonce?: string
@@ -17,7 +17,7 @@ export interface HydraAuthorizationCodeClientOptions {
1717
clientSecret: string
1818
tokenHost: string
1919
redirectUri: string
20-
scope?: string[]
20+
scope?: Array<string>
2121
}
2222

2323
export interface HydraAuthorizationCodeResult {
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './hydra-authorization-code.interfaces'
2-
export * from './hydra-authorization-code.client'
3-
export * from './state.utils'
1+
export * from './hydra-authorization-code.interfaces.js'
2+
export * from './hydra-authorization-code.client.js'
3+
export * from './state.utils.js'

packages/hydra-oauth-client/src/state.utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { State } from './hydra-authorization-code.interfaces'
1+
import type { State } from './hydra-authorization-code.interfaces.js'
22

3-
export const serializeState = (state: State) =>
3+
export const serializeState = (state: State): string =>
44
Buffer.from(JSON.stringify(state)).toString('base64')
55

66
export const parseState = (state: string): State | null => {
77
try {
8-
return JSON.parse(Buffer.from(state, 'base64').toString())
8+
return JSON.parse(Buffer.from(state, 'base64').toString()) as State
99
} catch {
1010
// TODO: log error
1111

packages/hydra-oauth-client/src/stub.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { describe } from '@jest/globals'
2+
import { expect } from '@jest/globals'
3+
import { it } from '@jest/globals'
4+
15
describe('stub', () => {
26
it('should be false', () => {
37
expect(false).toBeFalsy()

packages/kratos-session/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@atls/kratos-session",
33
"version": "0.0.1",
44
"license": "BSD 3-Clause",
5+
"type": "module",
56
"main": "src/index.ts",
67
"files": [
78
"dist"
@@ -17,8 +18,8 @@
1718
},
1819
"devDependencies": {
1920
"@types/react": "18.2.22",
20-
"@types/react-dom": "18.2.7",
21-
"next": "13.5.2"
21+
"@types/react-dom": "18.3.1",
22+
"next": "13.5.7"
2223
},
2324
"publishConfig": {
2425
"access": "public",

packages/kratos-session/src/get-kratos.client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Configuration } from '@ory/kratos-client'
22
import { PublicApi } from '@ory/kratos-client'
33

4-
let kratos
4+
let kratos: PublicApi
55

6-
export const getKratosClient = () => {
6+
export const getKratosClient = (): PublicApi => {
77
if (!kratos) {
88
kratos = new PublicApi(new Configuration({ basePath: process.env.KRATOS_PUBLIC_URL }))
99
}

packages/kratos-session/src/get-kratos.session.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { NextApiRequest } from 'next'
1+
import type { Session } from '@ory/kratos-client'
2+
import type { IncomingMessage } from 'http'
3+
import type { NextApiRequest } from 'next'
24

3-
import { Logger } from '@atls/logger'
4-
import { Session } from '@ory/kratos-client'
5-
import { IncomingMessage } from 'http'
5+
import { Logger } from '@atls/logger'
66

7-
import { getKratosClient } from './get-kratos.client'
7+
import { getKratosClient } from './get-kratos.client.js'
88

99
const logger = new Logger('getKratosSession')
1010

@@ -30,11 +30,14 @@ export const getKratosSession = async (
3030
try {
3131
const kratos = getKratosClient()
3232

33+
// @ts-expect-error
34+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
3335
const { data: session } = await kratos.whoami(cookie, authorization)
3436

3537
if (session) {
3638
logger.debug(session)
3739

40+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
3841
return session
3942
}
4043
} catch (error) {

packages/kratos-session/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './get-kratos.session'
2-
export * from './get-kratos.client'
1+
export * from './get-kratos.session.js'
2+
export * from './get-kratos.client.js'

0 commit comments

Comments
 (0)