@owlmeans/auth 0.1.18-rc.4 → 0.1.18-rc.41

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.
Files changed (92) hide show
  1. package/README.md +289 -45
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/auth/SKILL.md +135 -32
  4. package/agent-meta/skills/auth-protocol/SKILL.md +165 -90
  5. package/build/allowance/index.d.ts +1 -1
  6. package/build/allowance/index.d.ts.map +1 -1
  7. package/build/allowance/index.js +1 -1
  8. package/build/allowance/index.js.map +1 -1
  9. package/build/allowance/model.d.ts.map +1 -1
  10. package/build/allowance/model.js +4 -2
  11. package/build/allowance/model.js.map +1 -1
  12. package/build/allowance/schemas.d.ts +7 -0
  13. package/build/allowance/schemas.d.ts.map +1 -0
  14. package/build/allowance/schemas.js +62 -0
  15. package/build/allowance/schemas.js.map +1 -0
  16. package/build/auth/index.d.ts +1 -1
  17. package/build/auth/index.d.ts.map +1 -1
  18. package/build/auth/index.js +1 -1
  19. package/build/auth/index.js.map +1 -1
  20. package/build/auth/model.js +2 -2
  21. package/build/auth/model.js.map +1 -1
  22. package/build/auth/schemas.d.ts +8 -0
  23. package/build/auth/schemas.d.ts.map +1 -0
  24. package/build/auth/schemas.js +55 -0
  25. package/build/auth/schemas.js.map +1 -0
  26. package/build/consts.d.ts +16 -1
  27. package/build/consts.d.ts.map +1 -1
  28. package/build/consts.js +17 -2
  29. package/build/consts.js.map +1 -1
  30. package/build/entity/index.d.ts +1 -1
  31. package/build/entity/index.js +1 -1
  32. package/build/entity/profile.js +2 -2
  33. package/build/entity/profile.js.map +1 -1
  34. package/build/entity/schemas.d.ts +4 -0
  35. package/build/entity/schemas.d.ts.map +1 -0
  36. package/build/entity/schemas.js +21 -0
  37. package/build/entity/schemas.js.map +1 -0
  38. package/build/errors.d.ts +15 -0
  39. package/build/errors.d.ts.map +1 -1
  40. package/build/errors.js +19 -0
  41. package/build/errors.js.map +1 -1
  42. package/build/helper/types.d.ts +22 -0
  43. package/build/helper/types.d.ts.map +1 -0
  44. package/build/helper/types.js +2 -0
  45. package/build/helper/types.js.map +1 -0
  46. package/build/helper.d.ts +9 -3
  47. package/build/helper.d.ts.map +1 -1
  48. package/build/helper.js +30 -17
  49. package/build/helper.js.map +1 -1
  50. package/build/index.d.ts +1 -0
  51. package/build/index.d.ts.map +1 -1
  52. package/build/index.js.map +1 -1
  53. package/build/permission/index.d.ts +1 -1
  54. package/build/permission/index.d.ts.map +1 -1
  55. package/build/permission/index.js +1 -1
  56. package/build/permission/index.js.map +1 -1
  57. package/build/permission/schemas.d.ts +6 -0
  58. package/build/permission/schemas.d.ts.map +1 -0
  59. package/build/permission/schemas.js +27 -0
  60. package/build/permission/schemas.js.map +1 -0
  61. package/build/rely/index.d.ts +1 -1
  62. package/build/rely/index.d.ts.map +1 -1
  63. package/build/rely/index.js +1 -1
  64. package/build/rely/index.js.map +1 -1
  65. package/build/rely/schemas.d.ts +4 -0
  66. package/build/rely/schemas.d.ts.map +1 -0
  67. package/build/rely/schemas.js +12 -0
  68. package/build/rely/schemas.js.map +1 -0
  69. package/build/supervisor.d.ts +0 -8
  70. package/build/supervisor.d.ts.map +1 -1
  71. package/build/types.d.ts +23 -1
  72. package/build/types.d.ts.map +1 -1
  73. package/package.json +3 -3
  74. package/src/allowance/index.ts +1 -1
  75. package/src/allowance/{model.ts → schemas.ts} +6 -4
  76. package/src/auth/index.ts +1 -1
  77. package/src/auth/{model.ts → schemas.ts} +3 -3
  78. package/src/consts.ts +20 -2
  79. package/src/entity/index.ts +1 -1
  80. package/src/entity/{profile.ts → schemas.ts} +3 -3
  81. package/src/errors.ts +22 -1
  82. package/src/helper/types.ts +22 -0
  83. package/src/helper.ts +44 -21
  84. package/src/index.ts +1 -0
  85. package/src/permission/index.ts +1 -1
  86. package/src/rely/index.ts +1 -1
  87. package/src/supervisor.ts +0 -8
  88. package/src/types.ts +30 -7
  89. package/tests/errors.spec.ts +19 -0
  90. package/build/.gitkeep +0 -0
  91. /package/src/permission/{model.ts → schemas.ts} +0 -0
  92. /package/src/rely/{model.ts → schemas.ts} +0 -0
package/build/types.d.ts CHANGED
@@ -32,7 +32,17 @@ export interface ProfilePayload extends Authorization {
32
32
  groups?: string[];
33
33
  }
34
34
  export interface Authorization {
35
- entityId?: string;
35
+ /**
36
+ * The organization entity's slug — renameable, and the ONLY entity value that travels on the
37
+ * wire: in tokens, in URLs and in request bodies.
38
+ *
39
+ * It is deliberately NOT a database key. An implementation that stores things per organization
40
+ * keeps its own stable `entityId` on its records and resolves this slug to it at the boundary,
41
+ * so a rename costs one registry write instead of a migration of every collection, object name
42
+ * and third-party record that ever referenced the organization. Basic auth knows nothing about
43
+ * that id — see `@owlmeans/auth-common`'s entity resolver for the contract that does.
44
+ */
45
+ entitySlug?: string;
36
46
  scopes: string[];
37
47
  permissions?: PermissionSet[];
38
48
  attributes?: AttributeSet[];
@@ -55,6 +65,10 @@ export interface Auth extends AuthPayload {
55
65
  token: string;
56
66
  isUser: boolean;
57
67
  createdAt: Date;
68
+ /** Opaque session registry key; it is not an organization identifier. */
69
+ sessionId?: string;
70
+ /** Monotonic authorization revision supplied by the session registry. */
71
+ authorizationVersion?: number;
58
72
  }
59
73
  export interface PermissionSet {
60
74
  scope: string;
@@ -78,4 +92,12 @@ export interface AllowanceResponse {
78
92
  export interface AuthToken {
79
93
  token: string;
80
94
  }
95
+ /**
96
+ * Shape of the value packed into `AuthCredentials.credential` by the supervisor
97
+ * web plugin: the client `salt` plus the `signature` over `buildSupervisorPayload`.
98
+ */
99
+ export interface SupervisorCredentialPayload {
100
+ salt: string;
101
+ signature: string;
102
+ }
81
103
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE3C,MAAM,WAAW,eAAgB,SAAQ,WAAW;IA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,CAAA;CAClB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAA;CACd;AAED;;;GAGG;AACH,MAAM,WAAW,2BAA2B;IAC1C,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAA;CAClB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/auth",
3
- "version": "0.1.18-rc.4",
3
+ "version": "0.1.18-rc.41",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -22,7 +22,7 @@
22
22
  }
23
23
  },
24
24
  "dependencies": {
25
- "@owlmeans/error": "^0.1.18-rc.4",
25
+ "@owlmeans/error": "^0.1.18-rc.38",
26
26
  "ajv-formats": "^3.0.1"
27
27
  },
28
28
  "peerDependencies": {
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "devDependencies": {
32
32
  "@owlmeans/dep-config": "workspace:*",
33
- "@types/bun": "^1.3.14",
33
+ "@types/bun": "^1.4.2",
34
34
  "nodemon": "^3.1.14",
35
35
  "typescript": "^7.0.2"
36
36
  },
@@ -1,2 +1,2 @@
1
1
 
2
- export * from './model.js'
2
+ export * from './schemas.js'
@@ -1,11 +1,11 @@
1
1
  import type { JSONSchemaType } from 'ajv'
2
2
  import type { AllowanceRequest, Auth, AuthCredentials, AuthPayload } from '../types.js'
3
3
  import {
4
- AuthRoleSchema, EntityValueSchema, GroupValueSchema, IdValueSchema, DateSchema,
4
+ AuthRoleSchema, EntitySlugValueSchema, GroupValueSchema, IdValueSchema, DateSchema,
5
5
  ScopeValueSchema, TypeNameSchema
6
6
  } from '../consts.js'
7
- import { AuthPayloadSchema } from '../auth/model.js'
8
- import { AttributeSetSchema, PermissionSetSchema } from '../permission/model.js'
7
+ import { AuthPayloadSchema } from '../auth/schemas.js'
8
+ import { AttributeSetSchema, PermissionSetSchema } from '../permission/schemas.js'
9
9
 
10
10
  export const PartialAuthPayloadSchema: JSONSchemaType<Omit<Partial<AuthPayload>, "type">> = {
11
11
  type: 'object',
@@ -16,7 +16,7 @@ export const PartialAuthPayloadSchema: JSONSchemaType<Omit<Partial<AuthPayload>,
16
16
  profileId: { ...IdValueSchema, nullable: true },
17
17
  expiresAt: { ...DateSchema, nullable: true },
18
18
  groups: { type: 'array', items: GroupValueSchema, nullable: true },
19
- entityId: { ...EntityValueSchema, nullable: true },
19
+ entitySlug: { ...EntitySlugValueSchema, nullable: true },
20
20
  scopes: { type: 'array', items: ScopeValueSchema, nullable: true },
21
21
  permissions: { type: 'array', nullable: true, items: PermissionSetSchema },
22
22
  attributes: { type: 'array', nullable: true, items: AttributeSetSchema },
@@ -58,6 +58,8 @@ export const AuthSchema: JSONSchemaType<Auth> = {
58
58
  token: { type: 'string', minLength: 32, maxLength: 1024 },
59
59
  isUser: { type: 'boolean' },
60
60
  createdAt: DateSchema,
61
+ sessionId: { type: 'string', minLength: 16, maxLength: 128, nullable: true },
62
+ authorizationVersion: { type: 'integer', minimum: 1, nullable: true },
61
63
  expiresAt: { ...DateSchema, nullable: true },
62
64
  ...AuthPayloadSchema.properties,
63
65
  },
package/src/auth/index.ts CHANGED
@@ -1,2 +1,2 @@
1
1
 
2
- export * from './model.js'
2
+ export * from './schemas.js'
@@ -1,15 +1,15 @@
1
1
  import type { JSONSchemaType } from 'ajv'
2
2
  import type { AuthPayload, AuthToken, Authorization, ProfilePayload } from '../types.js'
3
3
  import {
4
- EntityValueSchema, EnumValueSchema, GroupValueSchema, IdValueSchema, DateSchema, ScopeValueSchema,
4
+ EntitySlugValueSchema, EnumValueSchema, GroupValueSchema, IdValueSchema, DateSchema, ScopeValueSchema,
5
5
  TypeNameSchema
6
6
  } from '../consts.js'
7
- import { AttributeSetSchema, PermissionSetSchema } from '../permission/model.js'
7
+ import { AttributeSetSchema, PermissionSetSchema } from '../permission/schemas.js'
8
8
 
9
9
  export const AuthorizationSchema: JSONSchemaType<Authorization> = {
10
10
  type: 'object',
11
11
  properties: {
12
- entityId: { ...EntityValueSchema, nullable: true },
12
+ entitySlug: { ...EntitySlugValueSchema, nullable: true },
13
13
  scopes: { type: 'array', items: ScopeValueSchema },
14
14
  permissions: { type: 'array', nullable: true, items: PermissionSetSchema },
15
15
  attributes: { type: 'array', nullable: true, items: AttributeSetSchema },
package/src/consts.ts CHANGED
@@ -98,7 +98,10 @@ export const ResourceValueSchema: JSONSchemaType<string> = { type: 'string', min
98
98
 
99
99
  export const AttributeValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 8, maxLength: 128 }
100
100
 
101
- export const EntityValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 3, maxLength: 256 }
101
+ export const EntitySlugValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 3, maxLength: 256 }
102
+
103
+ /** @deprecated The organization value on the wire is a slug — use {@link EntitySlugValueSchema}. */
104
+ export const EntityValueSchema: JSONSchemaType<string> = EntitySlugValueSchema
102
105
 
103
106
  export const GroupValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 1, maxLength: 128 }
104
107
 
@@ -115,15 +118,30 @@ export const AuthRoleSchema: JSONSchemaType<AuthRole> = { type: 'string', enum:
115
118
  export const DISPATCHER = 'dispatcher'
116
119
  export const DISPATCHER_AUTHEN = `${DISPATCHER}:authentication`
117
120
 
121
+ /**
122
+ * The login window opened one level up by an application that cannot complete the flow where it is.
123
+ *
124
+ * A separate entrypoint from {@link DISPATCHER} because it is never a `redirect_uri` — the provider
125
+ * always returns to the dispatcher, whatever page opened the window — and because a logout has to
126
+ * run somewhere that does not start the authorization machine.
127
+ */
128
+ export const DISPATCHER_SURROGATE = `${DISPATCHER}:surrogate`
129
+
118
130
  export const AUTHEN_RELY = `${AUTHEN}-rely`
119
131
 
120
132
  export const AUTH_HEADER = 'authorization'
121
133
  export const AUTH_QUERY = 'token'
122
- export const ENTITY_QUERY = 'entity'
134
+ export const ENTITY_QUERY = 'entitySlug'
123
135
  export const PROFILE_QUERY = 'profile'
124
136
 
125
137
  export const MOD_RECAPTCHA = '_external:re-captcha'
126
138
  export const CMOD_RECAPTCHA = `_client-${MOD_RECAPTCHA.slice(1)}`
127
139
 
140
+ /**
141
+ * The guard of a route a reCAPTCHA-proven guest may call once: `Authorization: RE-CAPTCHA <token>`,
142
+ * the token the auth manager's reCAPTCHA plugin issued (`makeReCaptchaGuard` in server-auth).
143
+ */
144
+ export const RECAPTCHA_GUARD = 'guard:re-captcha'
145
+
128
146
  export const GUEST_ID = '__guest'
129
147
  export const RELY_3RD = 'rely'
@@ -1,2 +1,2 @@
1
1
 
2
- export * from './profile.js'
2
+ export * from './schemas.js'
@@ -1,8 +1,8 @@
1
1
 
2
2
  import type { JSONSchemaType } from 'ajv'
3
3
  import type { Profile } from '../types.js'
4
- import { EntityValueSchema, IdValueSchema } from '../consts.js'
5
- import { PermissionSetSchema, AttributeSetSchema } from '../permission/model.js'
4
+ import { EntitySlugValueSchema, IdValueSchema } from '../consts.js'
5
+ import { PermissionSetSchema, AttributeSetSchema } from '../permission/schemas.js'
6
6
 
7
7
  export const ProfileSchema: JSONSchemaType<Profile> = {
8
8
  type: 'object',
@@ -12,7 +12,7 @@ export const ProfileSchema: JSONSchemaType<Profile> = {
12
12
  credential: { type: 'string', nullable: true },
13
13
  secret: { type: 'string', nullable: true },
14
14
  groups: { type: 'array', items: { type: 'string' }, nullable: true },
15
- entityId: {...EntityValueSchema, nullable: true},
15
+ entitySlug: {...EntitySlugValueSchema, nullable: true},
16
16
  scopes: { type: 'array', items: { type: 'string' } },
17
17
  permissions: { type: 'array', items: PermissionSetSchema, nullable: true },
18
18
  attributes: { type: 'array', items: AttributeSetSchema, nullable: true },
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,32 +1,55 @@
1
- import type { Auth, AuthCredentials, AuthToken } from './types.js'
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/model.js'
4
+ import { AuthCredentialsSchema, AuthSchema } from './allowance/schemas.js'
5
+ import type { AuthHelper } from './helper/types.js'
5
6
 
6
- const ajv = new Ajv({ strict: false })
7
- // @TODO There is some serious type mismatch probably because of wrong versions resolution
8
- formatsPlugin(ajv as any)
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
- export const verifyAuth = (auth: Auth): boolean => {
11
- const validate = ajv.compile(AuthSchema)
12
+ const verifyAuth = (auth: Auth): boolean => {
13
+ const validate = ajv.compile(AuthSchema)
12
14
 
13
- return validate(auth)
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)
15
27
 
16
- export const verifyAuthCredentials = (auth: AuthCredentials): boolean => {
17
- const validate = ajv.compile(AuthCredentialsSchema)
28
+ const isAuthCredentials = (auth: unknown): auth is AuthCredentials =>
29
+ typeof auth === 'object' && auth != null
30
+ && ("challenge" in auth) && ("credential" in auth)
18
31
 
19
- return validate(auth)
32
+ const isAuthToken = (auth: unknown): auth is AuthToken =>
33
+ typeof auth === 'object' && auth != null
34
+ && ("token" in auth) && typeof auth.token === 'string'
35
+
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 isAuth = (auth: unknown): auth is Auth =>
23
- typeof auth === 'object' && auth != null
24
- && ("token" in auth) && ("isUser" in auth)
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)
25
49
 
26
- export const isAuthCredentials = (auth: unknown): auth is AuthCredentials =>
27
- typeof auth === 'object' && auth != null
28
- && ("challenge" in auth) && ("credential" in auth)
50
+ /** @deprecated compat:factory-refactor — use `authHelper.isAuthToken(…)` */
51
+ export const isAuthToken = (auth: unknown): auth is AuthToken => authHelper.isAuthToken(auth)
29
52
 
30
- export const isAuthToken = (auth: unknown): auth is AuthToken =>
31
- typeof auth === 'object' && auth != null
32
- && ("token" in auth) && typeof auth.token === 'string'
53
+ /** @deprecated compat:factory-refactor — use `authHelper.entitySlugOf(…)` */
54
+ export const entitySlugOf = (payload?: Partial<Authorization> | null): string | undefined =>
55
+ authHelper.entitySlugOf(payload)
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
 
2
2
  export type * from './types.js'
3
+ export type * from './helper/types.js'
3
4
  export * from './errors.js'
4
5
  export * from './consts.js'
5
6
  export * from './helper.js'
@@ -1,2 +1,2 @@
1
1
 
2
- export * from './model.js'
2
+ export * from './schemas.js'
package/src/rely/index.ts CHANGED
@@ -1,3 +1,3 @@
1
1
 
2
2
  export type * from './types.js'
3
- export * from './model.js'
3
+ export * from './schemas.js'
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
@@ -15,7 +15,7 @@ export interface AuthCredentials extends AuthPayload {
15
15
  ProfilePayload:
16
16
  groups?: string[]
17
17
  Authorization:
18
- entityId?: string
18
+ entitySlug?: string
19
19
  scopes: string[]
20
20
  permissions?: PermissionSet[]
21
21
  attributes?: AttributeSet[]
@@ -47,7 +47,7 @@ export interface AuthPayload extends ProfilePayload {
47
47
  ProfilePayload:
48
48
  groups?: string[]
49
49
  Authorization:
50
- entityId?: string
50
+ entitySlug?: string
51
51
  scopes: string[]
52
52
  permissions?: PermissionSet[]
53
53
  attributes?: AttributeSet[]
@@ -65,7 +65,7 @@ export interface ProfilePayload extends Authorization {
65
65
  groups?: string[]
66
66
  /**
67
67
  Authorization:
68
- entityId?: string
68
+ entitySlug?: string
69
69
  scopes: string[]
70
70
  permissions?: PermissionSet[]
71
71
  attributes?: AttributeSet[]
@@ -75,7 +75,17 @@ export interface ProfilePayload extends Authorization {
75
75
  }
76
76
 
77
77
  export interface Authorization {
78
- entityId?: string
78
+ /**
79
+ * The organization entity's slug — renameable, and the ONLY entity value that travels on the
80
+ * wire: in tokens, in URLs and in request bodies.
81
+ *
82
+ * It is deliberately NOT a database key. An implementation that stores things per organization
83
+ * keeps its own stable `entityId` on its records and resolves this slug to it at the boundary,
84
+ * so a rename costs one registry write instead of a migration of every collection, object name
85
+ * and third-party record that ever referenced the organization. Basic auth knows nothing about
86
+ * that id — see `@owlmeans/auth-common`'s entity resolver for the contract that does.
87
+ */
88
+ entitySlug?: string
79
89
  scopes: string[]
80
90
  permissions?: PermissionSet[]
81
91
  attributes?: AttributeSet[]
@@ -98,7 +108,7 @@ export interface Profile extends ProfilePayload {
98
108
  ProfilePayload:
99
109
  groups?: string[]
100
110
  Authorization:
101
- entityId?: string
111
+ entitySlug?: string
102
112
  scopes: string[]
103
113
  permissions?: PermissionSet[]
104
114
  attributes?: AttributeSet[]
@@ -111,6 +121,10 @@ export interface Auth extends AuthPayload {
111
121
  token: string
112
122
  isUser: boolean
113
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
114
128
  /**
115
129
  AuthPayload:
116
130
  type: string
@@ -122,7 +136,7 @@ export interface Auth extends AuthPayload {
122
136
  ProfilePayload:
123
137
  groups?: string[]
124
138
  Authorization:
125
- entityId?: string
139
+ entitySlug?: string
126
140
  scopes: string[]
127
141
  permissions?: PermissionSet[]
128
142
  attributes?: AttributeSet[]
@@ -160,7 +174,7 @@ export interface AllowanceRequest extends Partial<AuthPayload> {
160
174
  ProfilePayload:
161
175
  groups?: string[]
162
176
  Authorization:
163
- entityId?: string
177
+ entitySlug?: string
164
178
  scopes?: string[]
165
179
  permissions?: PermissionSet[]
166
180
  attributes?: AttributeSet[]
@@ -176,3 +190,12 @@ export interface AllowanceResponse {
176
190
  export interface AuthToken {
177
191
  token: string
178
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
+ }
@@ -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
+ })
package/build/.gitkeep DELETED
File without changes
File without changes
File without changes