This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
/
VendorOAuthConnector.js
167 lines (153 loc) · 7.75 KB
/
VendorOAuthConnector.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const { OAuthConnector } = require('@fusebit/oauth-connector');
const Superagent = require('superagent');
class VendorOAuthConnector extends OAuthConnector {
constructor() {
super();
}
/**
* Called during connector initialization to allow the connector to register additional, application-specific
* routes on the provided Express router.
* @param {*} Express router
*/
onCreate(app) {
super.onCreate(app);
// // Add a route that enables an authorized caller to send a hypothetical notification to a specific user.
// app.post(
// '/notification/:vendorUserId',
// this.authorize({
// action: 'function:execute',
// resourceFactory: (req) =>
// `/account/${req.fusebit.accountId}/subscription/${req.fusebit.subscriptionId}/boundary/${
// req.fusebit.boundaryId
// }/function/${req.fusebit.functionId}/notification/${encodeURIComponent(req.params.vendorUserId)}/`,
// }),
// async (req, res, next) => {
// // Check if the user with the specifed ID has previously authenticated
// const userContext = await this.getUser(req.fusebit, req.params.vendorUserId);
// if (!userContext) {
// res.status(404);
// } else {
// // Ensure the access token for the user is current
// const tokenContext = await this.ensureAccessToken(req.fusebit, userContext);
// console.log(
// `Sending notification to user ${req.params.vendorUserId} using access token ${tokenContext.access_token}`,
// req.body
// );
// res.status(200);
// }
// res.end();
// }
// );
}
/**
* Called when the entire connector is being deleted. Override the logic in this method to remove
* any artifacts created during the lifetime of this connector (e.g. Fusebit functions, storage).
* @param {FusebitContext} fusebitContext The Fusebit context
*/
async onDelete(fusebitContext) {
await super.onDelete(fusebitContext);
}
/**
* Called after a new user successfuly completed a configuration flow and was persisted in the system. This extensibility
* point allows for creation of any artifacts required to serve this new user, for example creation of additional
* Fusebit functions.
* @param {FusebitContext} fusebitContext The Fusebit context of the request
* @param {*} userContext The user context representing the vendor's user. Contains vendorToken and vendorUserProfile, representing responses
* from getAccessToken and getUserProfile, respectively.
*/
async onNewUser(fusebitContext, userContext) {
await super.onNewUser(fusebitContext, userContext);
}
/**
* Creates the fully formed web authorization URL to start the authorization flow.
* @param {FusebitContext} fusebitContext The Fusebit context of the request
* @param {string} state The value of the OAuth state parameter.
* @param {string} redirectUri The callback URL to redirect to after the authorization flow.
*/
async getAuthorizationUrl(fusebitContext, state, redirectUri) {
return super.getAuthorizationUrl(fusebitContext, state, redirectUri);
}
/**
* Exchanges the OAuth authorization code for the access and refresh tokens.
* @param {FusebitContext} fusebitContext The Fusebit context of the request
* @param {string} authorizationCode The authorization_code supplied to the OAuth callback upon successful authorization flow.
* @param {string} redirectUri The redirect_uri value Fusebit used to start the authorization flow.
*/
async getAccessToken(fusebitContext, authorizationCode, redirectUri) {
return super.getAccessToken(fusebitContext, authorizationCode, redirectUri);
}
/**
* Obtains a new access token using refresh token.
* @param {FusebitContext} fusebitContext The Fusebit context of the request
* @param {*} tokenContext An object representing the result of the getAccessToken call. It contains refresh_token.
* @param {string} redirectUri The redirect_uri value Fusebit used to start the authorization flow.
*/
async refreshAccessToken(fusebitContext, tokenContext, redirectUri) {
return super.refreshAccessToken(fusebitContext, tokenContext, redirectUri);
}
/**
* Obtains the user profile given a freshly completed authorization flow. User profile will be stored along the token
* context.
* @param {*} tokenContext An object representing the result of the getAccessToken call. It contains access_token.
*/
async getUserProfile(tokenContext) {
// const response = await Superagent.get('https://contoso.auth0.com/userinfo')
// .set('Authorization', `Bearer ${tokenContext.access_token}`);
// return response.body;
return super.getUserProfile(tokenContext);
}
/**
* Returns a string uniquely identifying the user in vendor's system. Typically this is a property of
* userContext.vendorUserProfile. Default implementation is opportunistically returning userContext.vendorUserProfile.id
* if it exists.
* @param {*} userContext The user context representing the vendor's user. Contains vendorToken and vendorUserProfile, representing responses
* from getAccessToken and getUserProfile, respectively.
*/
async getUserId(userContext) {
// return userContext.vendorUserProfile.sub;
return super.getUserId(userContext);
}
/**
* Returns the HTML of the web page that initiates the authorization flow to the authorizationUrl. Return
* undefined if you don't want to present any HTML to the user but instead redirect the user directly to
* the authorizationUrl.
* @param {FusebitContext} fusebitContext The Fusebit context of the request
* @param {string} authorizationUrl The fully formed authorization url to redirect the user to
*/
async getAuthorizationPageHtml(fusebitContext, authorizationUrl) {
return super.getAuthorizationPageHtml(fusebitContext, authorizationUrl);
}
/**
* Saves user context in storage for future use. This is also an opportunity to ensure any user-specific arifacts
* are created, for example a Fusebit function.
* @param {FusebitContext} fusebitContext The Fusebit context of the request
* @param {*} userContext The user context representing the vendor's user. Contains vendorToken and vendorUserProfile, representing responses
* from getAccessToken and getUserProfile, respectively.
*/
async saveUser(fusebitContext, userContext) {
await super.saveUser(fusebitContext, userContext);
}
/**
* Deletes all artifacts associated with a vendor user. This is an opportunity to remove any artifacts created in
* saveUser, for example Fusebit functions.
* @param {FusebitContext} fusebitContext The Fusebit context
* @param {string} vendorUserId The vendor user id
* @param {string} vendorId If specified, vendorUserId represents the identity of the user in another system.
* The vendorId must correspond to an entry in userContext.foreignOAuthIdentities.
*/
async deleteUser(fusebitContext, vendorUserId, vendorId) {
await super.deleteUser(fusebitContext, vendorUserId, vendorId);
}
/**
* Gets the health status of the user
* @param {FusebitContext} fusebitContext The Fusebit context of the request
* @param {*} userContext The user context representing the vendor's user. Contains vendorToken and vendorUserProfile, representing responses
* from getAccessToken and getUserProfile, respectively.
*/
async getHealth(fusebitContext, userContext) {
// Perform any application-specific checks in the user represented by userContext and return an object with 'status'
// and optionally 'body' properties.
return { status: 200 };
}
}
module.exports.VendorOAuthConnector = VendorOAuthConnector;