@owlmeans/auth 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.
Files changed (86) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +321 -0
  3. package/build/.gitkeep +0 -0
  4. package/build/allowance/index.d.ts +2 -0
  5. package/build/allowance/index.d.ts.map +1 -0
  6. package/build/allowance/index.js +2 -0
  7. package/build/allowance/index.js.map +1 -0
  8. package/build/allowance/model.d.ts +7 -0
  9. package/build/allowance/model.d.ts.map +1 -0
  10. package/build/allowance/model.js +57 -0
  11. package/build/allowance/model.js.map +1 -0
  12. package/build/auth/index.d.ts +2 -0
  13. package/build/auth/index.d.ts.map +1 -0
  14. package/build/auth/index.js +2 -0
  15. package/build/auth/index.js.map +1 -0
  16. package/build/auth/model.d.ts +8 -0
  17. package/build/auth/model.d.ts.map +1 -0
  18. package/build/auth/model.js +55 -0
  19. package/build/auth/model.js.map +1 -0
  20. package/build/consts.d.ts +69 -0
  21. package/build/consts.d.ts.map +1 -0
  22. package/build/consts.js +100 -0
  23. package/build/consts.js.map +1 -0
  24. package/build/entity/index.d.ts +2 -0
  25. package/build/entity/index.d.ts.map +1 -0
  26. package/build/entity/index.js +2 -0
  27. package/build/entity/index.js.map +1 -0
  28. package/build/entity/profile.d.ts +4 -0
  29. package/build/entity/profile.d.ts.map +1 -0
  30. package/build/entity/profile.js +21 -0
  31. package/build/entity/profile.js.map +1 -0
  32. package/build/errors.d.ts +54 -0
  33. package/build/errors.d.ts.map +1 -0
  34. package/build/errors.js +105 -0
  35. package/build/errors.js.map +1 -0
  36. package/build/helper.d.ts +7 -0
  37. package/build/helper.d.ts.map +1 -0
  38. package/build/helper.js +21 -0
  39. package/build/helper.js.map +1 -0
  40. package/build/index.d.ts +10 -0
  41. package/build/index.d.ts.map +1 -0
  42. package/build/index.js +9 -0
  43. package/build/index.js.map +1 -0
  44. package/build/permission/index.d.ts +2 -0
  45. package/build/permission/index.d.ts.map +1 -0
  46. package/build/permission/index.js +2 -0
  47. package/build/permission/index.js.map +1 -0
  48. package/build/permission/model.d.ts +6 -0
  49. package/build/permission/model.d.ts.map +1 -0
  50. package/build/permission/model.js +27 -0
  51. package/build/permission/model.js.map +1 -0
  52. package/build/rely/index.d.ts +3 -0
  53. package/build/rely/index.d.ts.map +1 -0
  54. package/build/rely/index.js +2 -0
  55. package/build/rely/index.js.map +1 -0
  56. package/build/rely/model.d.ts +4 -0
  57. package/build/rely/model.d.ts.map +1 -0
  58. package/build/rely/model.js +12 -0
  59. package/build/rely/model.js.map +1 -0
  60. package/build/rely/types.d.ts +7 -0
  61. package/build/rely/types.d.ts.map +1 -0
  62. package/build/rely/types.js +2 -0
  63. package/build/rely/types.js.map +1 -0
  64. package/build/types.d.ts +81 -0
  65. package/build/types.d.ts.map +1 -0
  66. package/build/types.js +2 -0
  67. package/build/types.js.map +1 -0
  68. package/package.json +38 -0
  69. package/src/allowance/index.ts +2 -0
  70. package/src/allowance/model.ts +65 -0
  71. package/src/auth/index.ts +2 -0
  72. package/src/auth/model.ts +64 -0
  73. package/src/consts.ts +121 -0
  74. package/src/entity/index.ts +2 -0
  75. package/src/entity/profile.ts +24 -0
  76. package/src/errors.ts +132 -0
  77. package/src/helper.ts +32 -0
  78. package/src/index.ts +10 -0
  79. package/src/permission/index.ts +2 -0
  80. package/src/permission/model.ts +31 -0
  81. package/src/rely/index.ts +3 -0
  82. package/src/rely/model.ts +14 -0
  83. package/src/rely/types.ts +7 -0
  84. package/src/types.ts +178 -0
  85. package/tsconfig.json +14 -0
  86. package/tsconfig.tsbuildinfo +1 -0
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@owlmeans/auth",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "scripts": {
6
+ "build": "tsc -b",
7
+ "dev": "sleep 24 && nodemon -e ts,tsx,json --watch src --exec \"tsc -p ./tsconfig.json\"",
8
+ "watch": "tsc -b -w --preserveWatchOutput --pretty"
9
+ },
10
+ "main": "build/index.js",
11
+ "module": "build/index.js",
12
+ "types": "build/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "import": "./build/index.js",
16
+ "require": "./build/index.js",
17
+ "default": "./build/index.js",
18
+ "module": "./build/index.js",
19
+ "types": "./build/index.d.ts"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "@owlmeans/error": "^0.1.0",
24
+ "ajv-formats": "^3.0.1"
25
+ },
26
+ "peerDependencies": {
27
+ "ajv": "*"
28
+ },
29
+ "devDependencies": {
30
+ "nodemon": "^3.1.7",
31
+ "npm-check": "^6.0.1",
32
+ "typescript": "^5.6.3"
33
+ },
34
+ "private": false,
35
+ "publishConfig": {
36
+ "access": "public"
37
+ }
38
+ }
@@ -0,0 +1,2 @@
1
+
2
+ export * from './model.js'
@@ -0,0 +1,65 @@
1
+ import type { JSONSchemaType } from 'ajv'
2
+ import type { AllowanceRequest, Auth, AuthCredentials, AuthPayload } from '../types.js'
3
+ import {
4
+ AuthRoleSchema, EntityValueSchema, GroupValueSchema, IdValueSchema, DateSchema,
5
+ ScopeValueSchema, TypeNameSchema
6
+ } from '../consts.js'
7
+ import { AuthPayloadSchema } from '../auth/model.js'
8
+ import { AttributeSetSchema, PermissionSetSchema } from '../permission/model.js'
9
+
10
+ export const PartialAuthPayloadSchema: JSONSchemaType<Omit<Partial<AuthPayload>, "type">> = {
11
+ type: 'object',
12
+ properties: {
13
+ role: { ...AuthRoleSchema, nullable: true },
14
+ source: { type: 'string', minLength: 1, maxLength: 1024, nullable: true },
15
+ userId: { ...IdValueSchema, nullable: true },
16
+ profileId: { ...IdValueSchema, nullable: true },
17
+ expiresAt: { ...DateSchema, nullable: true },
18
+ groups: { type: 'array', items: GroupValueSchema, nullable: true },
19
+ entityId: { ...EntityValueSchema, nullable: true },
20
+ scopes: { type: 'array', items: ScopeValueSchema, nullable: true },
21
+ permissions: { type: 'array', nullable: true, items: PermissionSetSchema },
22
+ attributes: { type: 'array', nullable: true, items: AttributeSetSchema },
23
+ permissioned: { type: 'boolean', nullable: true },
24
+ denormalized: { type: 'boolean', nullable: true }
25
+ },
26
+ required: [],
27
+ additionalProperties: false,
28
+ }
29
+
30
+ export const AllowanceRequestSchema: JSONSchemaType<AllowanceRequest> = {
31
+ type: 'object',
32
+ properties: {
33
+ type: TypeNameSchema,
34
+ ...PartialAuthPayloadSchema.properties,
35
+ } as any,
36
+ required: ['type'],
37
+ additionalProperties: false,
38
+ }
39
+
40
+ export const AuthCredentialsSchema: JSONSchemaType<AuthCredentials> = {
41
+ type: 'object',
42
+ properties: {
43
+ challenge: { type: 'string', minLength: 32, maxLength: 1024 },
44
+ credential: { type: 'string', minLength: 16, maxLength: 4096 },
45
+ publicKey: { type: 'string', minLength: 16, maxLength: 1024, nullable: true },
46
+ ...AuthPayloadSchema.properties
47
+ },
48
+ required: ['challenge', 'credential', ...AuthPayloadSchema.required],
49
+ additionalProperties: false,
50
+ }
51
+
52
+ export const AuthSchema: JSONSchemaType<Auth> = {
53
+ type: 'object',
54
+ properties: {
55
+ token: { type: 'string', minLength: 32, maxLength: 1024 },
56
+ isUser: { type: 'boolean' },
57
+ createdAt: DateSchema,
58
+ expiresAt: { ...DateSchema, nullable: true },
59
+ ...AuthPayloadSchema.properties,
60
+ },
61
+ required: [
62
+ 'token', 'isUser', 'createdAt', 'type', 'role', 'userId'
63
+ ],
64
+ additionalProperties: false,
65
+ }
@@ -0,0 +1,2 @@
1
+
2
+ export * from './model.js'
@@ -0,0 +1,64 @@
1
+ import type { JSONSchemaType } from 'ajv'
2
+ import type { AuthPayload, AuthToken, Authorization, ProfilePayload } from '../types.js'
3
+ import {
4
+ EntityValueSchema, EnumValueSchema, GroupValueSchema, IdValueSchema, DateSchema, ScopeValueSchema,
5
+ TypeNameSchema
6
+ } from '../consts.js'
7
+ import { AttributeSetSchema, PermissionSetSchema } from '../permission/model.js'
8
+
9
+ export const AuthorizationSchema: JSONSchemaType<Authorization> = {
10
+ type: 'object',
11
+ properties: {
12
+ entityId: { ...EntityValueSchema, nullable: true },
13
+ scopes: { type: 'array', items: ScopeValueSchema },
14
+ permissions: { type: 'array', nullable: true, items: PermissionSetSchema },
15
+ attributes: { type: 'array', nullable: true, items: AttributeSetSchema },
16
+ permissioned: { type: 'boolean', nullable: true },
17
+ denormalized: { type: 'boolean', nullable: true }
18
+ },
19
+ required: ['scopes'],
20
+ additionalProperties: false,
21
+ }
22
+
23
+ export const ProfilePayloadSchema: JSONSchemaType<ProfilePayload> = {
24
+ type: 'object',
25
+ properties: {
26
+ ...AuthorizationSchema.properties,
27
+ groups: { type: 'array', nullable: true, items: GroupValueSchema },
28
+ } as any,
29
+ required: ['scopes'],
30
+ additionalProperties: false,
31
+ }
32
+
33
+ export const AuthPayloadSchema: JSONSchemaType<AuthPayload> = {
34
+ type: 'object',
35
+ properties: {
36
+ ...ProfilePayloadSchema.properties,
37
+ type: TypeNameSchema,
38
+ role: EnumValueSchema,
39
+ source: { type: 'string', minLength: 1, maxLength: 1024, nullable: true },
40
+ userId: { ...IdValueSchema, nullable: true },
41
+ profileId: { ...IdValueSchema, nullable: true },
42
+ expiresAt: { ...DateSchema, nullable: true },
43
+ } as any,
44
+ required: ['scopes', 'role', 'type'],
45
+ additionalProperties: false,
46
+ }
47
+
48
+ export const AuthTokenSchema: JSONSchemaType<AuthToken> = {
49
+ type: 'object',
50
+ properties: {
51
+ token: { type: 'string', minLength: 32, maxLength: 1024 },
52
+ },
53
+ required: ['token'],
54
+ additionalProperties: false,
55
+ }
56
+
57
+ export const OptionalAuthTokenSchema: JSONSchemaType<Partial<AuthToken>> = {
58
+ type: 'object',
59
+ properties: {
60
+ token: { type: 'string', minLength: 32, maxLength: 1024, nullable: true },
61
+ },
62
+ required: [],
63
+ additionalProperties: false,
64
+ }
package/src/consts.ts ADDED
@@ -0,0 +1,121 @@
1
+ import type { JSONSchemaType } from 'ajv'
2
+
3
+ export const INCLUDE = '+'
4
+ export const EXCLUDE = '-'
5
+ export const WILDCARD = '*'
6
+ export const DELIMITER = ':'
7
+
8
+ export const ALL_SCOPES = '*'
9
+ export const AUTH_SCOPE = '__auth'
10
+
11
+ export enum AuthRole {
12
+ User = 'user',
13
+ Guest = 'guest',
14
+ Service = 'service',
15
+ System = 'system',
16
+ Admin = 'admin',
17
+ Superuser = 'superuser',
18
+ Blocked = 'blocked'
19
+ }
20
+
21
+ export enum AuthenticationType {
22
+ BasicEd25519 = 'basic-ed25519',
23
+ OneTimeToken = 'one-time-token',
24
+ ReCaptcha = 're-captcha',
25
+ // @TODO think if it should be moved from here
26
+ // - it's too implementation specific
27
+ // OwlMeans specific WalletProvider authentication type.
28
+ // It's used to establish connection between the wallet and the rely service for further
29
+ // providing a rely connection.
30
+ // 1. It exactly does the following: authenticate wallet in the rely endpoint to get
31
+ // an auth token that allows to open rely connection as provider.
32
+ WalletDid = 'wallet-did',
33
+ // Doesn't have explicity plugin - is internal part of rely based plugins
34
+ // 2. It is exactly used by wallet consumer and provider inside rely connection
35
+ // to cross authenticate one another and establish connection.
36
+ RelyHandshake = 'rely-handshake',
37
+ // 3. It's used by wallet consumer to authenticate in the rely endpoint.
38
+ // After this authorization - the client gets a connection object that is capable
39
+ // to RPC wallet methods.
40
+ // ! Actually this is an internal protocol that isn't used directly but is used inside
41
+ // some specific authorization plugin (e.g. for registration or login).
42
+ // So actually it's quite similar to 'wallet-did', but from the client side.
43
+ // The difference is that 'wallet-did' is used by itself to provide RPC wallet methods.
44
+ WalletConsumer = 'wallet-consumer',
45
+ // Right now this authentication type is ephemeral cause there is no support of
46
+ // abstract non-owlmenas-backend-bound wallets
47
+ // 4p. Potentially it can be used by other wallets or some universal wallet protocol
48
+ // to fullfil the role of wallet provider.
49
+ // For OwlMeans Auth Manager Service (OwlMeans IAM) there is a propritary protocol
50
+ // that is used as provider type
51
+ WalletProvider = 'wallet-provider',
52
+ }
53
+
54
+ export enum AuthroizationType {
55
+ // Reserved to implement classic authentication token with ttled berear token.
56
+ AuthToken = 'auth-token',
57
+ // Main authorization token type for OwlMeans. The authentication token 'one-time-token'
58
+ // is used to be exchanged to this one by auth service.
59
+ Ed25519BasicToken = 'ed25519-basic-token',
60
+ // Implies that the request will contain signature of a known party to authorize request
61
+ // with authetnication header of this type.
62
+ Ed25519BasicSignature = 'ed25519-basic-signature'
63
+ }
64
+
65
+ export enum AuthenticationStage {
66
+ Error = 'error',
67
+ Init = 'init',
68
+ Allowence = 'allowence',
69
+ Authenticate = 'authenticate',
70
+ Authentication = 'auhtentication',
71
+ Authenticated = 'authenticated'
72
+ }
73
+
74
+
75
+ export const AUTHEN = 'authentication'
76
+ export const AUTHEN_INIT = `${AUTHEN}:init`
77
+ export const AUTHEN_AUTHEN = `${AUTHEN}:authenticate`
78
+
79
+ export const CAUTHEN = `client-${AUTHEN}`
80
+ export const CAUTHEN_AUTHEN = `${CAUTHEN}:authentication`
81
+ export const CAUTHEN_AUTHEN_DEFAULT = `${CAUTHEN}:authentication:default`
82
+ export const CAUTHEN_AUTHEN_TYPED = `${CAUTHEN}:authentication:typed`
83
+ export const CAUTHEN_FLOW_ENTER = `${CAUTHEN}:flow:enter`
84
+
85
+ export const ScopeValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 1, maxLength: 32 }
86
+
87
+ export const PermissionValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 8, maxLength: 128 }
88
+
89
+ export const ResourceValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 8, maxLength: 128 }
90
+
91
+ export const AttributeValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 8, maxLength: 128 }
92
+
93
+ export const EntityValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 3, maxLength: 256 }
94
+
95
+ export const GroupValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 1, maxLength: 128 }
96
+
97
+ export const TypeNameSchema: JSONSchemaType<string> = { type: 'string', minLength: 1, maxLength: 64 }
98
+
99
+ export const EnumValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 1, maxLength: 32 }
100
+
101
+ export const IdValueSchema: JSONSchemaType<string> = { type: 'string', minLength: 1, maxLength: 128 }
102
+
103
+ export const DateSchema: JSONSchemaType<Date> = { type: 'object', format: 'date-time', required: [] }
104
+
105
+ export const AuthRoleSchema: JSONSchemaType<AuthRole> = { type: 'string', enum: Object.values(AuthRole) }
106
+
107
+ export const DISPATCHER = 'dispatcher'
108
+ export const DISPATCHER_AUTHEN = `${DISPATCHER}:authentication`
109
+
110
+ export const AUTHEN_RELY = `${AUTHEN}-rely`
111
+
112
+ export const AUTH_HEADER = 'authorization'
113
+ export const AUTH_QUERY = 'token'
114
+ export const ENTITY_QUERY = 'entity'
115
+ export const PROFILE_QUERY = 'profile'
116
+
117
+ export const MOD_RECAPTCHA = '_external:re-captcha'
118
+ export const CMOD_RECAPTCHA = `_client-${MOD_RECAPTCHA.slice(1)}`
119
+
120
+ export const GUEST_ID = '__guest'
121
+ export const RELY_3RD = 'rely'
@@ -0,0 +1,2 @@
1
+
2
+ export * from './profile.js'
@@ -0,0 +1,24 @@
1
+
2
+ import type { JSONSchemaType } from 'ajv'
3
+ import type { Profile } from '../types.js'
4
+ import { EntityValueSchema, IdValueSchema } from '../consts.js'
5
+ import { PermissionSetSchema, AttributeSetSchema } from '../permission/model.js'
6
+
7
+ export const ProfileSchema: JSONSchemaType<Profile> = {
8
+ type: 'object',
9
+ properties: {
10
+ id: IdValueSchema,
11
+ name: { type: 'string', minLength: 0, maxLength: 128 },
12
+ credential: { type: 'string', nullable: true },
13
+ secret: { type: 'string', nullable: true },
14
+ groups: { type: 'array', items: { type: 'string' }, nullable: true },
15
+ entityId: {...EntityValueSchema, nullable: true},
16
+ scopes: { type: 'array', items: { type: 'string' } },
17
+ permissions: { type: 'array', items: PermissionSetSchema, nullable: true },
18
+ attributes: { type: 'array', items: AttributeSetSchema, nullable: true },
19
+ permissioned: { type: 'boolean', nullable: true },
20
+ denormalized: { type: 'boolean', nullable: true }
21
+ },
22
+ required: ['id', 'name', 'scopes'],
23
+ additionalProperties: false,
24
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,132 @@
1
+
2
+ import { ResilientError } from '@owlmeans/error'
3
+
4
+ export class AuthError extends ResilientError {
5
+ public static override typeName: string = 'AuthError'
6
+
7
+ constructor(message: string = 'error') {
8
+ super(AuthError.typeName, `auth:${message}`)
9
+ }
10
+ }
11
+
12
+ export class AuthUnknown extends AuthError {
13
+ public static override typeName: string = `${AuthError.typeName}Unknown`
14
+
15
+ constructor(message: string = 'error') {
16
+ super(`unknown:${message}`)
17
+ this.type = AuthUnknown.typeName
18
+ }
19
+ }
20
+
21
+ export class AuthManagerError extends AuthError {
22
+ public static override typeName: string = 'AuthManagerError'
23
+
24
+ constructor(message: string = 'error') {
25
+ super(`manager:${message}`)
26
+ this.type = AuthManagerError.typeName
27
+ }
28
+ }
29
+
30
+ export class AuthManagerUnsupported extends AuthManagerError {
31
+ public static override typeName: string = `${AuthManagerError.typeName}Unsupported`
32
+
33
+ constructor(message: string = 'error') {
34
+ super(`unsupported:${message}`)
35
+ this.type = AuthManagerUnsupported.typeName
36
+ }
37
+ }
38
+
39
+ export class AuthenFailed extends AuthManagerError {
40
+ public static override typeName: string = `${AuthManagerError.typeName}AuthenFailed`
41
+
42
+ constructor(message: string = 'error') {
43
+ super(`authen:${message}`)
44
+ this.type = AuthenFailed.typeName
45
+ }
46
+ }
47
+
48
+ export class AuthenExists extends AuthenFailed {
49
+ public static override typeName: string = `${AuthenFailed.typeName}Exists`
50
+
51
+ constructor(message: string = 'error') {
52
+ super(`exists:${message}`)
53
+ this.type = AuthenExists.typeName
54
+ }
55
+ }
56
+
57
+ export class AuthenPayloadError extends AuthenFailed {
58
+ public static override typeName: string = `${AuthenFailed.typeName}Payload`
59
+
60
+ constructor(message: string = 'error') {
61
+ super(`payload:${message}`)
62
+ this.type = AuthenPayloadError.typeName
63
+ }
64
+ }
65
+
66
+ export class AuthPluginError extends AuthManagerError {
67
+ public static override typeName: string = `${AuthManagerError.typeName}Plugin`
68
+
69
+ constructor(message: string = 'error') {
70
+ super(`plugin:${message}`)
71
+ this.type = AuthPluginError.typeName
72
+ }
73
+ }
74
+
75
+ export class TypeMissmatchError extends AuthPluginError {
76
+ public static override typeName: string = `${AuthPluginError.typeName}TypeMissmatch`
77
+
78
+ constructor(message: string = 'error') {
79
+ super(`missmatch:${message}`)
80
+ this.type = TypeMissmatchError.typeName
81
+ }
82
+ }
83
+
84
+ export class AuthorizationError extends AuthError {
85
+ public static override typeName: string = 'AuthorizationError'
86
+
87
+ constructor(message: string = 'error') {
88
+ super(`authorization:${message}`)
89
+ this.type = AuthorizationError.typeName
90
+ }
91
+ }
92
+
93
+ export class AuthForbidden extends AuthorizationError {
94
+ public static override typeName: string = `Forbidden${AuthorizationError.typeName}`
95
+
96
+ constructor(message: string = 'error') {
97
+ super(`forbidden:${message}`)
98
+ this.type = AuthForbidden.typeName
99
+ }
100
+ }
101
+
102
+ export class ProfileError extends AuthError {
103
+ public static override typeName: string = 'ProfileError'
104
+
105
+ constructor(message: string = 'error') {
106
+ super(`profile:${message}`)
107
+ this.type = ProfileError.typeName
108
+ }
109
+ }
110
+
111
+ export class ProfileConsistencyError extends ProfileError {
112
+ public static override typeName: string = `${ProfileError.typeName}Consistency`
113
+
114
+ constructor(message: string = 'error') {
115
+ super(`consistency:${message}`)
116
+ this.type = ProfileConsistencyError.typeName
117
+ }
118
+ }
119
+
120
+ ResilientError.registerErrorClass(AuthError)
121
+ ResilientError.registerErrorClass(AuthUnknown)
122
+ ResilientError.registerErrorClass(AuthManagerError)
123
+ ResilientError.registerErrorClass(AuthManagerUnsupported)
124
+ ResilientError.registerErrorClass(AuthenFailed)
125
+ ResilientError.registerErrorClass(AuthenExists)
126
+ ResilientError.registerErrorClass(AuthenPayloadError)
127
+ ResilientError.registerErrorClass(AuthPluginError)
128
+ ResilientError.registerErrorClass(TypeMissmatchError)
129
+ ResilientError.registerErrorClass(AuthorizationError)
130
+ AuthForbidden.registerErrorClass(AuthForbidden)
131
+ ResilientError.registerErrorClass(ProfileError)
132
+ ResilientError.registerErrorClass(ProfileConsistencyError)
package/src/helper.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { Auth, AuthCredentials, AuthToken } from './types.js'
2
+ import { Ajv } from 'ajv'
3
+ import formatsPlugin from 'ajv-formats'
4
+ import { AuthCredentialsSchema, AuthSchema } from './allowance/model.js'
5
+
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)
9
+
10
+ export const verifyAuth = (auth: Auth): boolean => {
11
+ const validate = ajv.compile(AuthSchema)
12
+
13
+ return validate(auth)
14
+ }
15
+
16
+ export const verifyAuthCredentials = (auth: AuthCredentials): boolean => {
17
+ const validate = ajv.compile(AuthCredentialsSchema)
18
+
19
+ return validate(auth)
20
+ }
21
+
22
+ export const isAuth = (auth: unknown): auth is Auth =>
23
+ typeof auth === 'object' && auth != null
24
+ && ("token" in auth) && ("isUser" in auth)
25
+
26
+ export const isAuthCredentials = (auth: unknown): auth is AuthCredentials =>
27
+ typeof auth === 'object' && auth != null
28
+ && ("challenge" in auth) && ("credential" in auth)
29
+
30
+ export const isAuthToken = (auth: unknown): auth is AuthToken =>
31
+ typeof auth === 'object' && auth != null
32
+ && ("token" in auth) && typeof auth.token === 'string'
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+
2
+ export type * from './types.js'
3
+ export * from './errors.js'
4
+ export * from './consts.js'
5
+ export * from './helper.js'
6
+ export * from './allowance/index.js'
7
+ export * from './auth/index.js'
8
+ export * from './permission/index.js'
9
+ export * from './rely/index.js'
10
+ export * from './entity/index.js'
@@ -0,0 +1,2 @@
1
+
2
+ export * from './model.js'
@@ -0,0 +1,31 @@
1
+ import { JSONSchemaType } from 'ajv'
2
+ import { AttributeSet, Capabilties, PermissionSet } from '../types.js'
3
+ import { AttributeValueSchema, ResourceValueSchema, ScopeValueSchema } from '../consts.js'
4
+
5
+ export const CapabiltiesSchema: JSONSchemaType<Capabilties> = {
6
+ type: 'object',
7
+ additionalProperties: { type: ['boolean', 'number'], nullable: true },
8
+ required: []
9
+ }
10
+
11
+ export const PermissionSetSchema: JSONSchemaType<PermissionSet> = {
12
+ type: 'object',
13
+ properties: {
14
+ title: { type: 'string', nullable: true, minLength: 1, maxLength: 128 },
15
+ scope: ScopeValueSchema,
16
+ permissions: CapabiltiesSchema,
17
+ resources: { type: 'array', items: ResourceValueSchema, nullable: true }
18
+ },
19
+ required: ['scope', 'permissions'],
20
+ additionalProperties: false,
21
+ }
22
+
23
+ export const AttributeSetSchema: JSONSchemaType<AttributeSet> = {
24
+ type: 'object',
25
+ properties: {
26
+ scope: ScopeValueSchema,
27
+ attributes: { type: 'array', items: AttributeValueSchema }
28
+ },
29
+ required: ['scope', 'attributes'],
30
+ additionalProperties: false,
31
+ }
@@ -0,0 +1,3 @@
1
+
2
+ export type * from './types.js'
3
+ export * from './model.js'
@@ -0,0 +1,14 @@
1
+ import type { JSONSchemaType } from 'ajv'
2
+ import type { RelyToken } from './types.js'
3
+
4
+ export const RelyChallengeSchema: JSONSchemaType<RelyToken> = {
5
+ type: 'object',
6
+ properties: {
7
+ pin: { type: 'string', nullable: true, minLength: 4, maxLength: 12 },
8
+ token: { type: 'string', nullable: true, minLength: 8, maxLength: 16 },
9
+ check: { type: 'string', nullable: true, minLength: 2, maxLength: 8 },
10
+ nonce: { type: 'string', minLength: 8, maxLength: 32 }
11
+ },
12
+ required: ['nonce'],
13
+ additionalProperties: false,
14
+ }
@@ -0,0 +1,7 @@
1
+
2
+ export interface RelyToken {
3
+ pin?: string
4
+ token?: string
5
+ check?: string
6
+ nonce: string
7
+ }