@toa.io/extensions.exposition 1.0.0-alpha.185 → 1.0.0-alpha.187

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.
@@ -31,7 +31,13 @@ export interface TransitInput {
31
31
  authority: string;
32
32
  username?: string;
33
33
  password?: string;
34
- id?: string;
34
+ inception?: boolean;
35
+ }
36
+ export interface AddInput {
37
+ id: string;
38
+ authority: string;
39
+ username: string;
40
+ password: string;
35
41
  }
36
42
  export interface IdOutput {
37
43
  id: string;
@@ -0,0 +1,14 @@
1
+ import type { AddInput, Context, IdOutput } from './types'
2
+ import type { Maybe } from '@toa.io/types'
3
+
4
+ export async function effect(input: AddInput, context: Context): Promise<Maybe<IdOutput>> {
5
+ return await context.local.transit({
6
+ input: {
7
+ authority: input.authority,
8
+ username: input.username,
9
+ password: input.password,
10
+ inception: true
11
+ },
12
+ query: { id: input.id }
13
+ })
14
+ }
@@ -24,9 +24,12 @@ export class Transition implements Operation {
24
24
  public async execute (input: TransitInput, object: Entity): Promise<Maybe<IdOutput>> {
25
25
  const existent = object._version !== 0
26
26
 
27
- if (existent)
27
+ if (existent) {
28
+ if (input.inception === true)
29
+ return ERR_EXISTS
30
+
28
31
  await this.tokens.revoke({ query: { id: object.id } })
29
- else
32
+ } else
30
33
  object.authority = input.authority
31
34
 
32
35
  if (input.username !== undefined) {
@@ -66,5 +69,6 @@ function invalid (value: string, expressions: RegExp[]): boolean {
66
69
  const ERR_PRINCIPAL_LOCKED = new Err('PRINCIPAL_LOCKED', 'Principal username cannot be changed')
67
70
  const ERR_INVALID_USERNAME = new Err('INVALID_USERNAME', 'Username is not meeting the requirements')
68
71
  const ERR_INVALID_PASSWORD = new Err('INVALID_PASSWORD', 'Password is not meeting the requirements')
72
+ const ERR_EXISTS = new Err('EXISTS', 'Basic credentials already exist')
69
73
 
70
74
  type Tokens = Context['remote']['identity']['tokens']
@@ -34,7 +34,14 @@ export interface TransitInput {
34
34
  authority: string
35
35
  username?: string
36
36
  password?: string
37
- id?: string
37
+ inception?: boolean
38
+ }
39
+
40
+ export interface AddInput {
41
+ id: string
42
+ authority: string
43
+ username: string
44
+ password: string
38
45
  }
39
46
 
40
47
  export interface IdOutput {
@@ -2,8 +2,8 @@ namespace: identity
2
2
  name: otp
3
3
 
4
4
  entity:
5
- schema: &entity
6
- properties:
5
+ schema:
6
+ properties: &properties
7
7
  authority: { type: string }
8
8
  username: { type: string }
9
9
  required: [authority, username]
@@ -14,7 +14,14 @@ entity:
14
14
 
15
15
  operations:
16
16
  issue:
17
- input: *entity
17
+ input:
18
+ properties:
19
+ <<: *properties
20
+ lifetime: &lifetime
21
+ type: number
22
+ minimum: 1
23
+ maximum: 36000 # 10 hours
24
+ required: [authority, username]
18
25
  authenticate:
19
26
  input:
20
27
  properties:
@@ -34,6 +41,8 @@ operations:
34
41
  configuration:
35
42
  schema:
36
43
  properties:
37
- lifetime: { type: number, default: 60 }
44
+ lifetime:
45
+ <<: *lifetime
46
+ default: 300
38
47
 
39
48
  stash: ~
@@ -3,6 +3,7 @@ export declare function effect(input: Input, context: Context): Promise<Output>;
3
3
  interface Input {
4
4
  authority: string;
5
5
  username: string;
6
+ lifetime?: number;
6
7
  }
7
8
  interface Output {
8
9
  code: string;
@@ -5,8 +5,9 @@ async function effect(input, context) {
5
5
  const { authority, username } = input;
6
6
  const code = Math.floor(100000 + Math.random() * 900000).toString();
7
7
  const key = `${authority}:${username}:${code}`;
8
- context.logs.debug('Issue OTP', { authority, username, code });
9
- await context.stash.set(key, 1, 'EX', context.configuration.lifetime);
8
+ const lifetime = input.lifetime ?? context.configuration.lifetime;
9
+ context.logs.debug('Issue OTP', { authority, username, code, lifetime });
10
+ await context.stash.set(key, 1, 'EX', lifetime);
10
11
  return { code };
11
12
  }
12
13
  exports.effect = effect;
@@ -1 +1 @@
1
- {"version":3,"file":"issue.js","sourceRoot":"","sources":["../source/issue.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,MAAM,CAAE,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