-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
accounts-oauth.ts
136 lines (112 loc) · 3.8 KB
/
accounts-oauth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
type User,
type DatabaseInterface,
type AuthenticationService,
type DatabaseInterfaceUser,
} from '@accounts/types';
import {
AccountsServer,
/*DatabaseInterfaceSessionsToken,*/ DatabaseInterfaceUserToken,
ServerHooks,
} from '@accounts/server';
import { type OAuthProviders } from './types/oauth-providers';
import { type OAuthUser } from './types/oauth-user';
import { ExecutionContext, Inject, Injectable } from 'graphql-modules';
import { OAuthProvidersToken } from './types/OAuthProviders.symbol';
const getRegistrationPayloadDefault = async (oauthUser: OAuthUser) => {
return {
email: oauthUser.email,
};
};
@Injectable({
global: true,
})
export class AccountsOauth<CustomUser extends User = User>
implements AuthenticationService<CustomUser>
{
@ExecutionContext() public context!: ExecutionContext;
public server!: AccountsServer;
public serviceName = 'oauth';
private db!: DatabaseInterfaceUser<CustomUser>;
// private dbSessions!: DatabaseInterfaceSessions;
constructor(
@Inject(OAuthProvidersToken) public oauthProviders: OAuthProviders,
@Inject(DatabaseInterfaceUserToken)
db?: DatabaseInterface<CustomUser> | DatabaseInterfaceUser<CustomUser>,
// @Inject(DatabaseInterfaceSessionsToken) dbSessions?: DatabaseInterfaceSessions,
@Inject(AccountsServer) server?: AccountsServer
) {
if (db) {
this.db = db;
// this.dbSessions = dbSessions ?? (db as DatabaseInterfaceSessions);
}
if (server) {
this.server = server;
}
}
private getOAuthProvider(providerName: string) {
const instanceOrCtor = this.oauthProviders[providerName];
// If it's a constructor we use dependency injection (GraphQL), otherwise we already have an instance (REST)
const provider =
typeof instanceOrCtor === 'function'
? this.context.injector.get(instanceOrCtor)
: instanceOrCtor;
if (!provider) {
throw new Error(`No OAuth provider with the name ${providerName} was registered.`);
}
return provider;
}
public getOAuthProviders(): OAuthProviders {
return this.oauthProviders;
}
public setUserStore(store: DatabaseInterfaceUser<CustomUser>) {
this.db = store;
}
public setSessionsStore() {
// Empty
}
public async authenticate(params: any): Promise<CustomUser | null> {
if (!params.provider) {
throw new Error('Invalid provider');
}
const userProvider = this.getOAuthProvider(params.provider);
if (typeof userProvider.authenticate !== 'function') {
throw new Error('Invalid provider');
}
const oauthUser = await userProvider.authenticate(params);
let user = await this.db.findUserByServiceId(params.provider, oauthUser.id);
if (!user && oauthUser.email) {
user = await this.db.findUserByEmail(oauthUser.email);
}
if (!user) {
try {
const userData = await (
userProvider.getRegistrationPayload || getRegistrationPayloadDefault
)(oauthUser);
const userId = await this.db.createUser(userData);
user = await this.db.findUserById(userId);
if (user == null) {
throw new Error(`Cannot find user ${userId}`);
}
if (this.server) {
await this.server.getHooks().emit(ServerHooks.CreateUserSuccess, user);
}
} catch (e) {
if (this.server) {
await this.server.getHooks().emit(ServerHooks.CreateUserError, user);
}
throw e;
}
}
await this.db.setService(user.id, params.provider, oauthUser);
return user;
}
public async unlink(userId: string, provider: string) {
if (!provider) {
throw new Error('Invalid provider');
}
// Check whether the service has been provided, throws if not
this.getOAuthProvider(provider);
await this.db.setService(userId, provider, null as any);
}
}