@furystack/rest-service 4.1.12 → 5.0.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 (51) hide show
  1. package/dist/actions/index.js +6 -6
  2. package/dist/actions/index.js.map +1 -1
  3. package/dist/api-manager.js +2 -2
  4. package/dist/api-manager.js.map +1 -1
  5. package/dist/endpoint-generators/create-delete-endpoint.spec.js +1 -1
  6. package/dist/endpoint-generators/create-delete-endpoint.spec.js.map +1 -1
  7. package/dist/endpoint-generators/create-get-collection-endpoint.spec.js +1 -1
  8. package/dist/endpoint-generators/create-get-collection-endpoint.spec.js.map +1 -1
  9. package/dist/endpoint-generators/create-get-entity-endpoint.spec.js +1 -1
  10. package/dist/endpoint-generators/create-get-entity-endpoint.spec.js.map +1 -1
  11. package/dist/endpoint-generators/create-patch-endpoint.spec.js +1 -1
  12. package/dist/endpoint-generators/create-patch-endpoint.spec.js.map +1 -1
  13. package/dist/endpoint-generators/create-post-endpoint.spec.js +1 -1
  14. package/dist/endpoint-generators/create-post-endpoint.spec.js.map +1 -1
  15. package/dist/endpoint-generators/index.js +5 -5
  16. package/dist/endpoint-generators/index.js.map +1 -1
  17. package/dist/http-authentication-settings.d.ts +1 -4
  18. package/dist/http-authentication-settings.d.ts.map +1 -1
  19. package/dist/http-authentication-settings.js +1 -3
  20. package/dist/http-authentication-settings.js.map +1 -1
  21. package/dist/http-user-context.d.ts +5 -8
  22. package/dist/http-user-context.d.ts.map +1 -1
  23. package/dist/http-user-context.js +19 -28
  24. package/dist/http-user-context.js.map +1 -1
  25. package/dist/http-user-context.spec.d.ts.map +1 -1
  26. package/dist/http-user-context.spec.js +33 -26
  27. package/dist/http-user-context.spec.js.map +1 -1
  28. package/dist/incoming-message-extensions.js +1 -1
  29. package/dist/incoming-message-extensions.js.map +1 -1
  30. package/dist/index.js +15 -15
  31. package/dist/index.js.map +1 -1
  32. package/dist/models/index.js +2 -2
  33. package/dist/models/index.js.map +1 -1
  34. package/dist/rest-service.integration.spec.js +1 -1
  35. package/dist/rest-service.integration.spec.js.map +1 -1
  36. package/dist/schema-validator/index.js +2 -2
  37. package/dist/schema-validator/index.js.map +1 -1
  38. package/dist/schema-validator/schema-validator.js +2 -2
  39. package/dist/schema-validator/schema-validator.js.map +1 -1
  40. package/dist/server-manager.js +2 -2
  41. package/dist/server-manager.js.map +1 -1
  42. package/dist/server-response-extensions.js +1 -1
  43. package/dist/server-response-extensions.js.map +1 -1
  44. package/dist/utils.js +1 -1
  45. package/dist/validate.integration.spec.js +1 -1
  46. package/dist/validate.integration.spec.js.map +1 -1
  47. package/dist/validate.js.map +1 -1
  48. package/package.json +12 -12
  49. package/src/http-authentication-settings.ts +2 -5
  50. package/src/http-user-context.spec.ts +44 -26
  51. package/src/http-user-context.ts +15 -25
@@ -4,6 +4,7 @@ import { Injectable } from '@furystack/inject'
4
4
  import { v1 } from 'uuid'
5
5
  import { HttpAuthenticationSettings } from './http-authentication-settings'
6
6
  import { DefaultSession } from 'models/default-session'
7
+ import { PasswordAuthenticator, UnauthenticatedError } from '@furystack/security'
7
8
 
8
9
  /**
9
10
  * Injectable UserContext for FuryStack HTTP Api
@@ -54,21 +55,16 @@ export class HttpUserContext {
54
55
  * @returns the authenticated User
55
56
  */
56
57
  public async authenticateUser(userName: string, password: string) {
57
- const match =
58
- (password &&
59
- password.length &&
60
- (await this.getUserStore().find({
61
- filter: {
62
- username: { $eq: userName },
63
- password: { $eq: this.authentication.hashMethod(password) },
64
- },
65
- }))) ||
66
- []
67
- if (match.length === 1) {
68
- const { password: pw, ...user } = match[0]
69
- return user
58
+ const result = await this.authenticator.checkPasswordForUser(userName, password)
59
+
60
+ if (!result.isValid) {
61
+ throw new UnauthenticatedError()
62
+ }
63
+ const user = await this.getUserStore().get(userName)
64
+ if (!user) {
65
+ throw new UnauthenticatedError()
70
66
  }
71
- throw Error('Failed to authenticate.')
67
+ return user
72
68
  }
73
69
 
74
70
  public async getCurrentUser(request: IncomingMessage) {
@@ -108,23 +104,16 @@ export class HttpUserContext {
108
104
  // Cookie auth
109
105
  const sessionId = this.getSessionIdFromRequest(request)
110
106
  if (sessionId) {
111
- const [session] = await this.getSessionStore().find({ filter: { sessionId: { $eq: sessionId } }, top: 2 })
107
+ const session = await this.getSessionStore().get(sessionId)
112
108
  if (session) {
113
- const userResult = await this.getUserStore().find({
114
- filter: {
115
- username: { $eq: session.username },
116
- },
117
- top: 2,
118
- })
119
- if (userResult.length === 1) {
120
- const { password, ...user } = userResult[0]
109
+ const user = await this.getUserStore().get(session.username)
110
+ if (user) {
121
111
  return user
122
112
  }
123
- throw Error('Inconsistent session result')
124
113
  }
125
114
  }
126
115
 
127
- throw Error('Failed to authenticate request')
116
+ throw new UnauthenticatedError()
128
117
  }
129
118
 
130
119
  /**
@@ -156,5 +145,6 @@ export class HttpUserContext {
156
145
  constructor(
157
146
  public readonly authentication: HttpAuthenticationSettings<User, DefaultSession>,
158
147
  private readonly storeManager: StoreManager,
148
+ private readonly authenticator: PasswordAuthenticator,
159
149
  ) {}
160
150
  }