@owlmeans/server-oidc-rp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +520 -0
- package/build/.gitkeep +0 -0
- package/build/actions/index.d.ts +3 -0
- package/build/actions/index.d.ts.map +1 -0
- package/build/actions/index.js +3 -0
- package/build/actions/index.js.map +1 -0
- package/build/actions/init.d.ts +3 -0
- package/build/actions/init.d.ts.map +1 -0
- package/build/actions/init.js +68 -0
- package/build/actions/init.js.map +1 -0
- package/build/actions/process.d.ts +17 -0
- package/build/actions/process.d.ts.map +1 -0
- package/build/actions/process.js +80 -0
- package/build/actions/process.js.map +1 -0
- package/build/auth/consts.d.ts +2 -0
- package/build/auth/consts.d.ts.map +1 -0
- package/build/auth/consts.js +2 -0
- package/build/auth/consts.js.map +1 -0
- package/build/auth/index.d.ts +2 -0
- package/build/auth/index.d.ts.map +1 -0
- package/build/auth/index.js +2 -0
- package/build/auth/index.js.map +1 -0
- package/build/auth/plugins/index.d.ts +2 -0
- package/build/auth/plugins/index.d.ts.map +1 -0
- package/build/auth/plugins/index.js +6 -0
- package/build/auth/plugins/index.js.map +1 -0
- package/build/auth/plugins/oidc-client.d.ts +50 -0
- package/build/auth/plugins/oidc-client.d.ts.map +1 -0
- package/build/auth/plugins/oidc-client.js +211 -0
- package/build/auth/plugins/oidc-client.js.map +1 -0
- package/build/auth/plugins/types.d.ts +1 -0
- package/build/auth/plugins/types.d.ts.map +1 -0
- package/build/auth/plugins/types.js +2 -0
- package/build/auth/plugins/types.js.map +1 -0
- package/build/consts.d.ts +20 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +20 -0
- package/build/consts.js.map +1 -0
- package/build/gate.d.ts +3 -0
- package/build/gate.d.ts.map +1 -0
- package/build/gate.js +22 -0
- package/build/gate.js.map +1 -0
- package/build/guard.d.ts +6 -0
- package/build/guard.d.ts.map +1 -0
- package/build/guard.js +13 -0
- package/build/guard.js.map +1 -0
- package/build/index.d.ts +8 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7 -0
- package/build/index.js.map +1 -0
- package/build/model/consts.d.ts +6 -0
- package/build/model/consts.d.ts.map +1 -0
- package/build/model/consts.js +7 -0
- package/build/model/consts.js.map +1 -0
- package/build/model/gate.d.ts +4 -0
- package/build/model/gate.d.ts.map +1 -0
- package/build/model/gate.js +98 -0
- package/build/model/gate.js.map +1 -0
- package/build/model/types.d.ts +25 -0
- package/build/model/types.d.ts.map +1 -0
- package/build/model/types.js +2 -0
- package/build/model/types.js.map +1 -0
- package/build/models.d.ts +1 -0
- package/build/models.d.ts.map +1 -0
- package/build/models.js +2 -0
- package/build/models.js.map +1 -0
- package/build/modules.d.ts +3 -0
- package/build/modules.d.ts.map +1 -0
- package/build/modules.js +12 -0
- package/build/modules.js.map +1 -0
- package/build/service.d.ts +3 -0
- package/build/service.d.ts.map +1 -0
- package/build/service.js +186 -0
- package/build/service.js.map +1 -0
- package/build/types.d.ts +55 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/build/utils/auth.d.ts +5 -0
- package/build/utils/auth.d.ts.map +1 -0
- package/build/utils/auth.js +48 -0
- package/build/utils/auth.js.map +1 -0
- package/build/utils/cache.d.ts +8 -0
- package/build/utils/cache.d.ts.map +1 -0
- package/build/utils/cache.js +7 -0
- package/build/utils/cache.js.map +1 -0
- package/build/utils/index.d.ts +5 -0
- package/build/utils/index.d.ts.map +1 -0
- package/build/utils/index.js +4 -0
- package/build/utils/index.js.map +1 -0
- package/build/utils/types.d.ts +10 -0
- package/build/utils/types.d.ts.map +1 -0
- package/build/utils/types.js +2 -0
- package/build/utils/types.js.map +1 -0
- package/build/utils/wrapped.d.ts +4 -0
- package/build/utils/wrapped.d.ts.map +1 -0
- package/build/utils/wrapped.js +3 -0
- package/build/utils/wrapped.js.map +1 -0
- package/build/wrapper.d.ts +3 -0
- package/build/wrapper.d.ts.map +1 -0
- package/build/wrapper.js +75 -0
- package/build/wrapper.js.map +1 -0
- package/package.json +71 -0
- package/src/actions/index.ts +3 -0
- package/src/actions/init.ts +90 -0
- package/src/actions/process.ts +113 -0
- package/src/auth/consts.ts +2 -0
- package/src/auth/index.ts +2 -0
- package/src/auth/plugins/index.ts +8 -0
- package/src/auth/plugins/oidc-client.ts +246 -0
- package/src/auth/plugins/types.ts +0 -0
- package/src/consts.ts +26 -0
- package/src/gate.ts +29 -0
- package/src/guard.ts +25 -0
- package/src/index.ts +7 -0
- package/src/model/consts.ts +6 -0
- package/src/model/gate.ts +129 -0
- package/src/model/types.ts +31 -0
- package/src/models.ts +1 -0
- package/src/modules.ts +30 -0
- package/src/service.ts +251 -0
- package/src/types.ts +64 -0
- package/src/utils/auth.ts +67 -0
- package/src/utils/cache.ts +12 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/types.ts +10 -0
- package/src/utils/wrapped.ts +6 -0
- package/src/wrapper.ts +92 -0
- package/tsconfig.json +15 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Resource } from '@owlmeans/resource'
|
|
2
|
+
import type { OIDCAuthCache } from './types.js'
|
|
3
|
+
import type { Config, Context } from '../types.js'
|
|
4
|
+
import { AUTH_CACHE } from '@owlmeans/server-auth'
|
|
5
|
+
import { OIDC_TOKEN_STORE } from '../consts.js'
|
|
6
|
+
|
|
7
|
+
export const cache = <C extends Config, T extends Context<C>>(context: T): Resource<OIDCAuthCache> =>
|
|
8
|
+
context.resource<Resource<OIDCAuthCache>>(AUTH_CACHE)
|
|
9
|
+
|
|
10
|
+
export const verifierId = (challenge: string) => `${OIDC_TOKEN_STORE}:verifier:${challenge}`
|
|
11
|
+
export const exchangeId = (exchange: string) => `${OIDC_TOKEN_STORE}:exchange:${exchange}`
|
|
12
|
+
export const managedId = (token: string) => `${OIDC_TOKEN_STORE}:token:${token}`
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AuthSpent } from '@owlmeans/server-auth'
|
|
2
|
+
import type { TokenSetParameters } from '../types.js'
|
|
3
|
+
|
|
4
|
+
export interface OIDCAuthCache extends AuthSpent {
|
|
5
|
+
verifier?: string
|
|
6
|
+
payload?: TokenSetParameters
|
|
7
|
+
client?: string
|
|
8
|
+
validated?: Date
|
|
9
|
+
entityId?: string
|
|
10
|
+
}
|
package/src/wrapper.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { AuthenFailed, AuthorizationError, AuthRole } from '@owlmeans/auth'
|
|
2
|
+
import type { Auth, AuthCredentials } from '@owlmeans/auth'
|
|
3
|
+
import { EnvelopeKind, makeEnvelopeModel } from '@owlmeans/basic-envelope'
|
|
4
|
+
import { createService } from '@owlmeans/context'
|
|
5
|
+
import type { OIDCTokenUpdate, WrappedOIDCService } from '@owlmeans/oidc'
|
|
6
|
+
import { OIDC_WRAPPED_TOKEN, WRAPPED_OIDC } from '@owlmeans/oidc'
|
|
7
|
+
import { cache, managedId } from './utils/cache.js'
|
|
8
|
+
import type { Config, Context, TokenSetParameters } from './types.js'
|
|
9
|
+
import { authService, OIDC_AUTH_LIFTETIME, OIDC_WRAP_FRESHNESS } from './consts.js'
|
|
10
|
+
import days from 'dayjs'
|
|
11
|
+
import type { ClientModule } from '@owlmeans/client-module'
|
|
12
|
+
import { TRUSTED } from '@owlmeans/config'
|
|
13
|
+
import { AUTH_SRV_KEY } from '@owlmeans/server-auth'
|
|
14
|
+
import { trust } from '@owlmeans/auth-common/utils'
|
|
15
|
+
|
|
16
|
+
export const makeOidcWrappingService = (): WrappedOIDCService => {
|
|
17
|
+
const service = createService<WrappedOIDCService>(WRAPPED_OIDC, {
|
|
18
|
+
update: async (token, thr) => {
|
|
19
|
+
const ctx = service.assertCtx<Config, Context>()
|
|
20
|
+
token = typeof token === 'string' ? token : token?.token
|
|
21
|
+
if (token == null) {
|
|
22
|
+
throw new AuthorizationError('token')
|
|
23
|
+
}
|
|
24
|
+
const [ , authentication ] = token.split(' ')
|
|
25
|
+
const envelope = makeEnvelopeModel<Auth>(authentication, EnvelopeKind.Token)
|
|
26
|
+
const user = envelope.message()
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const record = await cache(ctx).get(managedId(user.token))
|
|
30
|
+
if (record == null || record.payload == null) {
|
|
31
|
+
throw new AuthorizationError('record')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (record.validated != null && days(record.validated)
|
|
35
|
+
.add(OIDC_WRAP_FRESHNESS, 'milliseconds').isAfter()) {
|
|
36
|
+
return { token }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
record.validated = new Date()
|
|
40
|
+
|
|
41
|
+
const [ update ] = await ctx.module<ClientModule<OIDCTokenUpdate>>(authService.auth.update)
|
|
42
|
+
.call({ body: { token, tokenSet: record.payload } })
|
|
43
|
+
|
|
44
|
+
const updateEnvelope = makeEnvelopeModel<AuthCredentials>(update.token, EnvelopeKind.Token)
|
|
45
|
+
const authManager = await trust<Config, Context>(ctx, TRUSTED, AUTH_SRV_KEY)
|
|
46
|
+
if (!await updateEnvelope.verify(authManager.key)) {
|
|
47
|
+
throw new AuthenFailed()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const updatedAuth = updateEnvelope.message()
|
|
51
|
+
|
|
52
|
+
const updatedUser: Auth = {
|
|
53
|
+
...user,
|
|
54
|
+
role: updatedAuth.role ?? AuthRole.Guest,
|
|
55
|
+
userId: updatedAuth.userId ?? user.userId,
|
|
56
|
+
profileId: updatedAuth.profileId,
|
|
57
|
+
entityId: updatedAuth.entityId ?? user.entityId,
|
|
58
|
+
createdAt: new Date()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (updatedAuth.challenge !== user.token) {
|
|
62
|
+
await cache(ctx).delete(record)
|
|
63
|
+
updatedUser.token = updatedAuth.challenge
|
|
64
|
+
record.id = managedId(updatedUser.token)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
const trusted = await trust<Config, Context>(ctx, TRUSTED, ctx.cfg.alias ?? ctx.cfg.service)
|
|
69
|
+
const authorization = await makeEnvelopeModel<Auth>(OIDC_WRAPPED_TOKEN)
|
|
70
|
+
.send(updatedUser, null).sign(trusted.key, EnvelopeKind.Token)
|
|
71
|
+
|
|
72
|
+
record.payload = update.tokenSet as TokenSetParameters
|
|
73
|
+
|
|
74
|
+
await cache(ctx).save(record, { ttl: OIDC_AUTH_LIFTETIME / 1000 })
|
|
75
|
+
|
|
76
|
+
return { token: `${OIDC_WRAPPED_TOKEN.toUpperCase()} ${authorization}` }
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.error(err)
|
|
79
|
+
await cache(ctx).delete(managedId(user.token))
|
|
80
|
+
if (thr) {
|
|
81
|
+
if (err instanceof AuthorizationError) {
|
|
82
|
+
throw err
|
|
83
|
+
}
|
|
84
|
+
throw new AuthorizationError('unknown')
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return null
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
return service
|
|
92
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"../tsconfig.default.json",
|
|
4
|
+
],
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"rootDir": "./src/", /* Specify the root folder within your source files. */
|
|
7
|
+
"outDir": "./build/", /* Specify an output folder for all emitted files. */
|
|
8
|
+
"moduleResolution": "Bundler",
|
|
9
|
+
},
|
|
10
|
+
"exclude": [
|
|
11
|
+
"./dist/**/*",
|
|
12
|
+
"./build/**/*",
|
|
13
|
+
"./*.ts"
|
|
14
|
+
]
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/consts.ts","./src/gate.ts","./src/guard.ts","./src/index.ts","./src/models.ts","./src/modules.ts","./src/service.ts","./src/types.ts","./src/wrapper.ts","./src/actions/index.ts","./src/actions/init.ts","./src/actions/process.ts","./src/auth/consts.ts","./src/auth/index.ts","./src/auth/plugins/index.ts","./src/auth/plugins/oidc-client.ts","./src/auth/plugins/types.ts","./src/model/consts.ts","./src/model/gate.ts","./src/model/types.ts","./src/utils/auth.ts","./src/utils/cache.ts","./src/utils/index.ts","./src/utils/types.ts","./src/utils/wrapped.ts"],"version":"5.6.3"}
|