@owlmeans/auth 0.1.18-rc.8 → 0.1.18
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/README.md +289 -45
- package/agent-meta/manifest.json +3 -3
- package/agent-meta/skills/auth/SKILL.md +135 -32
- package/agent-meta/skills/auth-protocol/SKILL.md +165 -90
- package/build/allowance/index.d.ts +1 -1
- package/build/allowance/index.d.ts.map +1 -1
- package/build/allowance/index.js +1 -1
- package/build/allowance/index.js.map +1 -1
- package/build/allowance/schemas.d.ts +7 -0
- package/build/allowance/schemas.d.ts.map +1 -0
- package/build/allowance/schemas.js +62 -0
- package/build/allowance/schemas.js.map +1 -0
- package/build/auth/index.d.ts +1 -1
- package/build/auth/index.d.ts.map +1 -1
- package/build/auth/index.js +1 -1
- package/build/auth/index.js.map +1 -1
- package/build/auth/schemas.d.ts +8 -0
- package/build/auth/schemas.d.ts.map +1 -0
- package/build/auth/schemas.js +55 -0
- package/build/auth/schemas.js.map +1 -0
- package/build/consts.d.ts +5 -0
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +5 -0
- package/build/consts.js.map +1 -1
- package/build/entity/index.d.ts +1 -1
- package/build/entity/index.js +1 -1
- package/build/entity/schemas.d.ts +4 -0
- package/build/entity/schemas.d.ts.map +1 -0
- package/build/entity/schemas.js +21 -0
- package/build/entity/schemas.js.map +1 -0
- package/build/errors.d.ts +15 -0
- package/build/errors.d.ts.map +1 -1
- package/build/errors.js +19 -0
- package/build/errors.js.map +1 -1
- package/build/helper/types.d.ts +22 -0
- package/build/helper/types.d.ts.map +1 -0
- package/build/helper/types.js +2 -0
- package/build/helper/types.js.map +1 -0
- package/build/helper.d.ts +7 -11
- package/build/helper.d.ts.map +1 -1
- package/build/helper.js +30 -27
- package/build/helper.js.map +1 -1
- package/build/index.d.ts +1 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js.map +1 -1
- package/build/permission/index.d.ts +1 -1
- package/build/permission/index.d.ts.map +1 -1
- package/build/permission/index.js +1 -1
- package/build/permission/index.js.map +1 -1
- package/build/permission/schemas.d.ts +6 -0
- package/build/permission/schemas.d.ts.map +1 -0
- package/build/permission/schemas.js +27 -0
- package/build/permission/schemas.js.map +1 -0
- package/build/rely/index.d.ts +1 -1
- package/build/rely/index.d.ts.map +1 -1
- package/build/rely/index.js +1 -1
- package/build/rely/index.js.map +1 -1
- package/build/rely/schemas.d.ts +4 -0
- package/build/rely/schemas.d.ts.map +1 -0
- package/build/rely/schemas.js +12 -0
- package/build/rely/schemas.js.map +1 -0
- package/build/supervisor.d.ts +0 -8
- package/build/supervisor.d.ts.map +1 -1
- package/build/types.d.ts +12 -0
- package/build/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/allowance/index.ts +1 -1
- package/src/allowance/{model.ts → schemas.ts} +4 -2
- package/src/auth/index.ts +1 -1
- package/src/auth/{model.ts → schemas.ts} +1 -1
- package/src/consts.ts +6 -0
- package/src/entity/index.ts +1 -1
- package/src/entity/{profile.ts → schemas.ts} +1 -1
- package/src/errors.ts +22 -1
- package/src/helper/types.ts +22 -0
- package/src/helper.ts +44 -33
- package/src/index.ts +1 -0
- package/src/permission/index.ts +1 -1
- package/src/rely/index.ts +1 -1
- package/src/supervisor.ts +0 -8
- package/src/types.ts +13 -0
- package/tests/errors.spec.ts +19 -0
- /package/src/permission/{model.ts → schemas.ts} +0 -0
- /package/src/rely/{model.ts → schemas.ts} +0 -0
package/src/errors.ts
CHANGED
|
@@ -9,9 +9,17 @@ export class AuthError extends ResilientError {
|
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* The request names an authentication method, provider or identity this server does not know — an
|
|
14
|
+
* unregistered plugin type, a sign-in method a gate refuses, a request carrying no organization.
|
|
15
|
+
* That is the caller's request, not a crashed server, so it answers 400 (`httpStatus`, read by
|
|
16
|
+
* `@owlmeans/server-api`). It is outside the authorization family on purpose: a failed credential
|
|
17
|
+
* stays 401 (`AuthorizationError`) and a refused permission 403 (`AuthForbidden`).
|
|
18
|
+
*/
|
|
12
19
|
export class AuthUnknown extends AuthError {
|
|
13
20
|
public static override typeName: string = `${AuthError.typeName}Unknown`
|
|
14
|
-
|
|
21
|
+
public static httpStatus = 400
|
|
22
|
+
|
|
15
23
|
constructor(message: string = 'error') {
|
|
16
24
|
super(`unknown:${message}`)
|
|
17
25
|
this.type = AuthUnknown.typeName
|
|
@@ -27,6 +35,18 @@ export class AuthManagerError extends AuthError {
|
|
|
27
35
|
}
|
|
28
36
|
}
|
|
29
37
|
|
|
38
|
+
/** An authentication authority could not be consulted, so access must fail closed. */
|
|
39
|
+
export class AuthUnavailable extends AuthError {
|
|
40
|
+
public static override typeName: string = `${AuthError.typeName}Unavailable`
|
|
41
|
+
public static httpStatus = 503
|
|
42
|
+
public static allowServerErrorStatus = true
|
|
43
|
+
|
|
44
|
+
constructor(message: string = 'unavailable') {
|
|
45
|
+
super(`unavailable:${message}`)
|
|
46
|
+
this.type = AuthUnavailable.typeName
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
30
50
|
export class AuthManagerUnsupported extends AuthManagerError {
|
|
31
51
|
public static override typeName: string = `${AuthManagerError.typeName}Unsupported`
|
|
32
52
|
|
|
@@ -120,6 +140,7 @@ export class ProfileConsistencyError extends ProfileError {
|
|
|
120
140
|
ResilientError.registerErrorClass(AuthError)
|
|
121
141
|
ResilientError.registerErrorClass(AuthUnknown)
|
|
122
142
|
ResilientError.registerErrorClass(AuthManagerError)
|
|
143
|
+
ResilientError.registerErrorClass(AuthUnavailable)
|
|
123
144
|
ResilientError.registerErrorClass(AuthManagerUnsupported)
|
|
124
145
|
ResilientError.registerErrorClass(AuthenFailed)
|
|
125
146
|
ResilientError.registerErrorClass(AuthenExists)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Auth, AuthCredentials, AuthToken, Authorization } from '../types.js'
|
|
2
|
+
|
|
3
|
+
/** Checks of the authentication payloads that cross the wire. */
|
|
4
|
+
export interface AuthHelper {
|
|
5
|
+
/** Whether a value satisfies the `Auth` schema. */
|
|
6
|
+
verifyAuth: (auth: Auth) => boolean
|
|
7
|
+
/** Whether a value satisfies the `AuthCredentials` schema. */
|
|
8
|
+
verifyAuthCredentials: (auth: AuthCredentials) => boolean
|
|
9
|
+
isAuth: (auth: unknown) => auth is Auth
|
|
10
|
+
isAuthCredentials: (auth: unknown) => auth is AuthCredentials
|
|
11
|
+
isAuthToken: (auth: unknown) => auth is AuthToken
|
|
12
|
+
/**
|
|
13
|
+
* The organization slug carried by an auth payload, tolerating tokens minted before the field
|
|
14
|
+
* was named.
|
|
15
|
+
*
|
|
16
|
+
* Tokens outlive deployments: one signed with the previous release still arrives with the value
|
|
17
|
+
* under `entityId`, and it stays valid until it expires. Everything that reads the organization
|
|
18
|
+
* off a payload goes through here so that window needs no second code path — and so the day the
|
|
19
|
+
* fallback can be deleted is a single-line change rather than an audit.
|
|
20
|
+
*/
|
|
21
|
+
entitySlugOf: (payload?: Partial<Authorization> | null) => string | undefined
|
|
22
|
+
}
|
package/src/helper.ts
CHANGED
|
@@ -1,44 +1,55 @@
|
|
|
1
1
|
import type { Auth, AuthCredentials, AuthToken, Authorization } from './types.js'
|
|
2
2
|
import { Ajv } from 'ajv'
|
|
3
3
|
import formatsPlugin from 'ajv-formats'
|
|
4
|
-
import { AuthCredentialsSchema, AuthSchema } from './allowance/
|
|
4
|
+
import { AuthCredentialsSchema, AuthSchema } from './allowance/schemas.js'
|
|
5
|
+
import type { AuthHelper } from './helper/types.js'
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
export const createAuthHelper = (): AuthHelper => {
|
|
8
|
+
const ajv = new Ajv({ strict: false })
|
|
9
|
+
// @TODO There is some serious type mismatch probably because of wrong versions resolution
|
|
10
|
+
formatsPlugin(ajv as any)
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
const verifyAuth = (auth: Auth): boolean => {
|
|
13
|
+
const validate = ajv.compile(AuthSchema)
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
+
return validate(auth)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const verifyAuthCredentials = (auth: AuthCredentials): boolean => {
|
|
19
|
+
const validate = ajv.compile(AuthCredentialsSchema)
|
|
20
|
+
|
|
21
|
+
return validate(auth)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const isAuth = (auth: unknown): auth is Auth =>
|
|
25
|
+
typeof auth === 'object' && auth != null
|
|
26
|
+
&& ("token" in auth) && ("isUser" in auth)
|
|
27
|
+
|
|
28
|
+
const isAuthCredentials = (auth: unknown): auth is AuthCredentials =>
|
|
29
|
+
typeof auth === 'object' && auth != null
|
|
30
|
+
&& ("challenge" in auth) && ("credential" in auth)
|
|
15
31
|
|
|
16
|
-
|
|
17
|
-
|
|
32
|
+
const isAuthToken = (auth: unknown): auth is AuthToken =>
|
|
33
|
+
typeof auth === 'object' && auth != null
|
|
34
|
+
&& ("token" in auth) && typeof auth.token === 'string'
|
|
18
35
|
|
|
19
|
-
|
|
36
|
+
const entitySlugOf = (payload?: Partial<Authorization> | null): string | undefined =>
|
|
37
|
+
payload?.entitySlug ?? (payload as { entityId?: string } | null | undefined)?.entityId
|
|
38
|
+
|
|
39
|
+
return { verifyAuth, verifyAuthCredentials, isAuth, isAuthCredentials, isAuthToken, entitySlugOf }
|
|
20
40
|
}
|
|
21
41
|
|
|
22
|
-
export const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* The organization slug carried by an auth payload, tolerating tokens minted before the field
|
|
36
|
-
* was named.
|
|
37
|
-
*
|
|
38
|
-
* Tokens outlive deployments: one signed with the previous release still arrives with the value
|
|
39
|
-
* under `entityId`, and it stays valid until it expires. Everything that reads the organization
|
|
40
|
-
* off a payload goes through here so that window needs no second code path — and so the day the
|
|
41
|
-
* fallback can be deleted is a single-line change rather than an audit.
|
|
42
|
-
*/
|
|
42
|
+
export const authHelper = createAuthHelper()
|
|
43
|
+
|
|
44
|
+
/** @deprecated compat:factory-refactor — use `authHelper.isAuth(…)` */
|
|
45
|
+
export const isAuth = (auth: unknown): auth is Auth => authHelper.isAuth(auth)
|
|
46
|
+
|
|
47
|
+
/** @deprecated compat:factory-refactor — use `authHelper.isAuthCredentials(…)` */
|
|
48
|
+
export const isAuthCredentials = (auth: unknown): auth is AuthCredentials => authHelper.isAuthCredentials(auth)
|
|
49
|
+
|
|
50
|
+
/** @deprecated compat:factory-refactor — use `authHelper.isAuthToken(…)` */
|
|
51
|
+
export const isAuthToken = (auth: unknown): auth is AuthToken => authHelper.isAuthToken(auth)
|
|
52
|
+
|
|
53
|
+
/** @deprecated compat:factory-refactor — use `authHelper.entitySlugOf(…)` */
|
|
43
54
|
export const entitySlugOf = (payload?: Partial<Authorization> | null): string | undefined =>
|
|
44
|
-
|
|
55
|
+
authHelper.entitySlugOf(payload)
|
package/src/index.ts
CHANGED
package/src/permission/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './schemas.js'
|
package/src/rely/index.ts
CHANGED
package/src/supervisor.ts
CHANGED
|
@@ -14,11 +14,3 @@ export const buildSupervisorPayload = (
|
|
|
14
14
|
challenge: string, userId: string, salt: string
|
|
15
15
|
): { challenge: string, userId: string, salt: string } => ({ challenge, userId, salt })
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Shape of the value packed into `AuthCredentials.credential` by the supervisor
|
|
19
|
-
* web plugin: the client `salt` plus the `signature` over `buildSupervisorPayload`.
|
|
20
|
-
*/
|
|
21
|
-
export interface SupervisorCredentialPayload {
|
|
22
|
-
salt: string
|
|
23
|
-
signature: string
|
|
24
|
-
}
|
package/src/types.ts
CHANGED
|
@@ -121,6 +121,10 @@ export interface Auth extends AuthPayload {
|
|
|
121
121
|
token: string
|
|
122
122
|
isUser: boolean
|
|
123
123
|
createdAt: Date
|
|
124
|
+
/** Opaque session registry key; it is not an organization identifier. */
|
|
125
|
+
sessionId?: string
|
|
126
|
+
/** Monotonic authorization revision supplied by the session registry. */
|
|
127
|
+
authorizationVersion?: number
|
|
124
128
|
/**
|
|
125
129
|
AuthPayload:
|
|
126
130
|
type: string
|
|
@@ -186,3 +190,12 @@ export interface AllowanceResponse {
|
|
|
186
190
|
export interface AuthToken {
|
|
187
191
|
token: string
|
|
188
192
|
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Shape of the value packed into `AuthCredentials.credential` by the supervisor
|
|
196
|
+
* web plugin: the client `salt` plus the `signature` over `buildSupervisorPayload`.
|
|
197
|
+
*/
|
|
198
|
+
export interface SupervisorCredentialPayload {
|
|
199
|
+
salt: string
|
|
200
|
+
signature: string
|
|
201
|
+
}
|
package/tests/errors.spec.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
AuthorizationError,
|
|
6
6
|
AuthenFailed,
|
|
7
7
|
AuthenPayloadError,
|
|
8
|
+
AuthUnknown,
|
|
8
9
|
} from '@owlmeans/auth'
|
|
9
10
|
|
|
10
11
|
describe('@owlmeans/auth — error inheritance chain', () => {
|
|
@@ -49,3 +50,21 @@ describe('@owlmeans/auth — error inheritance chain', () => {
|
|
|
49
50
|
expect(thrown).toHaveLength(3)
|
|
50
51
|
})
|
|
51
52
|
})
|
|
53
|
+
|
|
54
|
+
describe('@owlmeans/auth — declared HTTP statuses', () => {
|
|
55
|
+
// An unknown method, provider or identity is the caller's request — never a crashed server (500).
|
|
56
|
+
test('AuthUnknown declares 400 and stays outside the authorization family', () => {
|
|
57
|
+
const err = new AuthUnknown('email-otp')
|
|
58
|
+
expect(AuthUnknown.httpStatus).toBe(400)
|
|
59
|
+
expect((err.constructor as { httpStatus?: unknown }).httpStatus).toBe(400)
|
|
60
|
+
expect(err).toBeInstanceOf(AuthError)
|
|
61
|
+
expect(err).not.toBeInstanceOf(AuthorizationError)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
// Their 401/403 comes from the family mapping in `@owlmeans/server-api`, never from a declaration.
|
|
65
|
+
test('the authorization family declares no status of its own', () => {
|
|
66
|
+
for (const refusal of [AuthorizationError, AuthForbidden]) {
|
|
67
|
+
expect([refusal.typeName, (refusal as { httpStatus?: unknown }).httpStatus]).toEqual([refusal.typeName, undefined])
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
})
|
|
File without changes
|
|
File without changes
|