@sphereon/ssi-express-support 0.30.1-unstable.4 → 0.30.1

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.
@@ -1,151 +1,151 @@
1
- import passport from 'passport'
2
- import * as u8a from 'uint8arrays'
3
- import { BearerUser, IStaticBearerVerifyOptions } from './types'
4
- export class StaticBearerAuth {
5
- private readonly strategy: string
6
- private static providers: Map<string, StaticBearerUserProvider> = new Map()
7
- private static verifyOptions: Map<string, IStaticBearerVerifyOptions | string> = new Map()
8
- private hashTokens?: boolean = false
9
-
10
- public static init(strategy: string, provider?: StaticBearerUserProvider) {
11
- return new StaticBearerAuth(strategy ?? 'bearer', provider ?? new MapBasedStaticBearerUserProvider(strategy))
12
- }
13
-
14
- private constructor(strategy: string, provider: StaticBearerUserProvider) {
15
- this.strategy = strategy
16
- if (StaticBearerAuth.providers.has(strategy)) {
17
- if (StaticBearerAuth.providers.get(strategy) !== provider) {
18
- throw Error('Cannot register another user provider for strategy: ' + strategy)
19
- }
20
- } else {
21
- StaticBearerAuth.providers.set(strategy, provider)
22
- }
23
- }
24
-
25
- get provider() {
26
- const provider = StaticBearerAuth.providers.get(this.strategy)
27
- if (!provider) {
28
- throw Error('Could not get user provider for ' + this.strategy)
29
- }
30
- return provider
31
- }
32
-
33
- withHashTokens(hashTokens: boolean): this {
34
- this.hashTokens = hashTokens
35
- return this
36
- }
37
-
38
- withUsers(users: BearerUser[] | BearerUser): this {
39
- this.addUser(users)
40
- return this
41
- }
42
-
43
- addUser(user: BearerUser[] | BearerUser): this {
44
- this.provider.addUser(user)
45
- return this
46
- }
47
-
48
- withVerifyOptions(options: IStaticBearerVerifyOptions | string): this {
49
- StaticBearerAuth.verifyOptions.set(this.strategy, options)
50
- return this
51
- }
52
-
53
- connectPassport() {
54
- const _provider = this.provider
55
- function findUser(token: string, cb: (error: any, user: any, options?: IStaticBearerVerifyOptions | string) => void) {
56
- const user = _provider.getUser(token)
57
- if (user) {
58
- return cb(null, user)
59
- }
60
- return cb('bearer token not found or incorrect', false)
61
- }
62
-
63
- import('passport-http-bearer')
64
- .then((httpBearer) => {
65
- const hashTokens = this.hashTokens ?? false
66
- passport.use(
67
- this.strategy,
68
- new httpBearer.Strategy({ passReqToCallback: false }, function (
69
- token: string,
70
- cb: (error: any, user: any, options?: IStaticBearerVerifyOptions | string) => void,
71
- ): void {
72
- if (hashTokens) {
73
- import('@noble/hashes/sha256')
74
- .then((hash) => {
75
- findUser(u8a.toString(hash.sha256(token)), cb)
76
- })
77
- .catch((error) => {
78
- console.log(`hash problem: ${error}`)
79
- throw Error('Did you include @noble/hashes in package.json?')
80
- })
81
- } else {
82
- findUser(token, cb)
83
- }
84
- }),
85
- )
86
- })
87
- .catch((error) => {
88
- console.log(`passport-http-bearer package problem: ${error}`)
89
- throw Error('Did you include passport-http-bearer in package.json?')
90
- })
91
- }
92
- }
93
-
94
- export interface StaticBearerUserProvider {
95
- strategy: string
96
-
97
- addUser(user: BearerUser | BearerUser[], hashToken?: boolean): void
98
-
99
- getUser(token: string): BearerUser | undefined
100
-
101
- hashedTokens?: boolean
102
- }
103
-
104
- export class MapBasedStaticBearerUserProvider implements StaticBearerUserProvider {
105
- private readonly _strategy: string
106
- private readonly _users: BearerUser[] = []
107
- private readonly _hashedTokens: boolean
108
-
109
- constructor(strategy: string, hashedTokens?: boolean) {
110
- this._strategy = strategy
111
- this._hashedTokens = hashedTokens ?? false
112
- }
113
-
114
- get users(): BearerUser[] {
115
- return this._users
116
- }
117
-
118
- get hashedTokens(): boolean {
119
- return this._hashedTokens
120
- }
121
-
122
- get strategy(): string {
123
- return this._strategy
124
- }
125
-
126
- getUser(token: string): BearerUser | undefined {
127
- return this.users.find((user) => user.token === token)
128
- }
129
-
130
- addUser(user: BearerUser | BearerUser[], hashToken?: boolean): void {
131
- const users = Array.isArray(user) ? user : [user]
132
- if (hashToken) {
133
- if (!this.hashedTokens) {
134
- throw Error('Cannot hash token, when hashed tokens is not enabled on the user provider for strategy ' + this.strategy)
135
- }
136
- import('@noble/hashes/sha256')
137
- .then((hash) => {
138
- users.forEach((user) => (user.token = u8a.toString(hash.sha256(user.token))))
139
- })
140
- .catch((error) => {
141
- console.log(`hash problem: ${error}`)
142
- throw Error('Did you include @noble/hashes in package.json?')
143
- })
144
- }
145
- this._users.push(...users)
146
- }
147
-
148
- getUsers(): BearerUser[] {
149
- return this._users
150
- }
151
- }
1
+ import passport from 'passport'
2
+ import * as u8a from 'uint8arrays'
3
+ import { BearerUser, IStaticBearerVerifyOptions } from './types'
4
+ export class StaticBearerAuth {
5
+ private readonly strategy: string
6
+ private static providers: Map<string, StaticBearerUserProvider> = new Map()
7
+ private static verifyOptions: Map<string, IStaticBearerVerifyOptions | string> = new Map()
8
+ private hashTokens?: boolean = false
9
+
10
+ public static init(strategy: string, provider?: StaticBearerUserProvider) {
11
+ return new StaticBearerAuth(strategy ?? 'bearer', provider ?? new MapBasedStaticBearerUserProvider(strategy))
12
+ }
13
+
14
+ private constructor(strategy: string, provider: StaticBearerUserProvider) {
15
+ this.strategy = strategy
16
+ if (StaticBearerAuth.providers.has(strategy)) {
17
+ if (StaticBearerAuth.providers.get(strategy) !== provider) {
18
+ throw Error('Cannot register another user provider for strategy: ' + strategy)
19
+ }
20
+ } else {
21
+ StaticBearerAuth.providers.set(strategy, provider)
22
+ }
23
+ }
24
+
25
+ get provider() {
26
+ const provider = StaticBearerAuth.providers.get(this.strategy)
27
+ if (!provider) {
28
+ throw Error('Could not get user provider for ' + this.strategy)
29
+ }
30
+ return provider
31
+ }
32
+
33
+ withHashTokens(hashTokens: boolean): this {
34
+ this.hashTokens = hashTokens
35
+ return this
36
+ }
37
+
38
+ withUsers(users: BearerUser[] | BearerUser): this {
39
+ this.addUser(users)
40
+ return this
41
+ }
42
+
43
+ addUser(user: BearerUser[] | BearerUser): this {
44
+ this.provider.addUser(user)
45
+ return this
46
+ }
47
+
48
+ withVerifyOptions(options: IStaticBearerVerifyOptions | string): this {
49
+ StaticBearerAuth.verifyOptions.set(this.strategy, options)
50
+ return this
51
+ }
52
+
53
+ connectPassport() {
54
+ const _provider = this.provider
55
+ function findUser(token: string, cb: (error: any, user: any, options?: IStaticBearerVerifyOptions | string) => void) {
56
+ const user = _provider.getUser(token)
57
+ if (user) {
58
+ return cb(null, user)
59
+ }
60
+ return cb('bearer token not found or incorrect', false)
61
+ }
62
+
63
+ import('passport-http-bearer')
64
+ .then((httpBearer) => {
65
+ const hashTokens = this.hashTokens ?? false
66
+ passport.use(
67
+ this.strategy,
68
+ new httpBearer.Strategy({ passReqToCallback: false }, function (
69
+ token: string,
70
+ cb: (error: any, user: any, options?: IStaticBearerVerifyOptions | string) => void,
71
+ ): void {
72
+ if (hashTokens) {
73
+ import('@noble/hashes/sha256')
74
+ .then((hash) => {
75
+ findUser(u8a.toString(hash.sha256(token)), cb)
76
+ })
77
+ .catch((error) => {
78
+ console.log(`hash problem: ${error}`)
79
+ throw Error('Did you include @noble/hashes in package.json?')
80
+ })
81
+ } else {
82
+ findUser(token, cb)
83
+ }
84
+ }),
85
+ )
86
+ })
87
+ .catch((error) => {
88
+ console.log(`passport-http-bearer package problem: ${error}`)
89
+ throw Error('Did you include passport-http-bearer in package.json?')
90
+ })
91
+ }
92
+ }
93
+
94
+ export interface StaticBearerUserProvider {
95
+ strategy: string
96
+
97
+ addUser(user: BearerUser | BearerUser[], hashToken?: boolean): void
98
+
99
+ getUser(token: string): BearerUser | undefined
100
+
101
+ hashedTokens?: boolean
102
+ }
103
+
104
+ export class MapBasedStaticBearerUserProvider implements StaticBearerUserProvider {
105
+ private readonly _strategy: string
106
+ private readonly _users: BearerUser[] = []
107
+ private readonly _hashedTokens: boolean
108
+
109
+ constructor(strategy: string, hashedTokens?: boolean) {
110
+ this._strategy = strategy
111
+ this._hashedTokens = hashedTokens ?? false
112
+ }
113
+
114
+ get users(): BearerUser[] {
115
+ return this._users
116
+ }
117
+
118
+ get hashedTokens(): boolean {
119
+ return this._hashedTokens
120
+ }
121
+
122
+ get strategy(): string {
123
+ return this._strategy
124
+ }
125
+
126
+ getUser(token: string): BearerUser | undefined {
127
+ return this.users.find((user) => user.token === token)
128
+ }
129
+
130
+ addUser(user: BearerUser | BearerUser[], hashToken?: boolean): void {
131
+ const users = Array.isArray(user) ? user : [user]
132
+ if (hashToken) {
133
+ if (!this.hashedTokens) {
134
+ throw Error('Cannot hash token, when hashed tokens is not enabled on the user provider for strategy ' + this.strategy)
135
+ }
136
+ import('@noble/hashes/sha256')
137
+ .then((hash) => {
138
+ users.forEach((user) => (user.token = u8a.toString(hash.sha256(user.token))))
139
+ })
140
+ .catch((error) => {
141
+ console.log(`hash problem: ${error}`)
142
+ throw Error('Did you include @noble/hashes in package.json?')
143
+ })
144
+ }
145
+ this._users.push(...users)
146
+ }
147
+
148
+ getUsers(): BearerUser[] {
149
+ return this._users
150
+ }
151
+ }