Skip to content

Commit e909bbe

Browse files
committed
Expanded jwt for oauth
1 parent b7c3d75 commit e909bbe

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/modules/membership/auth/AuthenticatedUser.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,41 @@ export class AuthenticatedUser extends BaseAuthenticatedUser {
8181
);
8282
}
8383

84+
public static getCombinedApiJwt(user: User, userChurch: LoginUserChurch) {
85+
const permList: string[] = [];
86+
87+
userChurch.apis?.forEach((api) => {
88+
api.permissions?.forEach((p) => {
89+
let permString = p.contentType + "_" + String(p.contentId).replace("null", "") + "_" + p.action;
90+
if (p.apiName) permString = p.apiName + "_" + p.contentType + "_" + String(p.contentId).replace("null", "") + "_" + p.action;
91+
permList.push(permString);
92+
});
93+
});
94+
95+
const groupIds: string[] = [];
96+
userChurch.groups?.forEach((g) => groupIds.push(g.id));
97+
const leaderGroupIds: string[] = [];
98+
userChurch.groups?.forEach((g) => { if (g.leader) leaderGroupIds.push(g.id); });
99+
100+
const options: SignOptions = { expiresIn: Environment.jwtExpiration as any };
101+
return jwt.sign(
102+
{
103+
id: user.id,
104+
email: user.email,
105+
firstName: user.firstName,
106+
lastName: user.lastName,
107+
churchId: userChurch.church.id,
108+
personId: userChurch.person.id,
109+
permissions: permList,
110+
groupIds,
111+
leaderGroupIds,
112+
membershipStatus: userChurch.person?.membershipStatus
113+
},
114+
Environment.jwtSecret,
115+
options
116+
);
117+
}
118+
84119
public static getUserJwt(user: User) {
85120
return jwt.sign({ id: user.id, email: user.email, firstName: user.firstName, lastName: user.lastName }, Environment.jwtSecret, { expiresIn: "180 days" });
86121
}

src/modules/membership/controllers/OAuthController.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,17 @@ export class OAuthController extends MembershipBaseController {
112112
apis: []
113113
};
114114

115+
// Load permissions for all APIs
116+
const permissionData = await this.repos.rolePermission.loadUserPermissionInChurch(user.id, church.id);
117+
if (permissionData) {
118+
loginUserChurch.apis = permissionData.apis;
119+
}
120+
115121
// Create access token
116122
const token: OAuthToken = {
117123
clientId: client.clientId,
118124
userChurchId: authCode.userChurchId,
119-
accessToken: AuthenticatedUser.getChurchJwt(user, loginUserChurch),
125+
accessToken: AuthenticatedUser.getCombinedApiJwt(user, loginUserChurch),
120126
refreshToken: UniqueIdHelper.shortId(),
121127
scopes: authCode.scopes,
122128
expiresAt: new Date(Date.now() + 60 * 60 * 1000 * 12) // 12 hours
@@ -156,11 +162,17 @@ export class OAuthController extends MembershipBaseController {
156162
apis: []
157163
};
158164

165+
// Load permissions for all APIs
166+
const permissionData = await this.repos.rolePermission.loadUserPermissionInChurch(user.id, church.id);
167+
if (permissionData) {
168+
loginUserChurch.apis = permissionData.apis;
169+
}
170+
159171
// Create new access token with proper JWT
160172
const token: OAuthToken = {
161173
clientId: client.clientId,
162174
userChurchId: oldToken.userChurchId,
163-
accessToken: AuthenticatedUser.getChurchJwt(user, loginUserChurch),
175+
accessToken: AuthenticatedUser.getCombinedApiJwt(user, loginUserChurch),
164176
refreshToken: UniqueIdHelper.shortId(),
165177
scopes: oldToken.scopes,
166178
expiresAt: new Date(Date.now() + 60 * 60 * 1000 * 12) // 12 hours
@@ -288,8 +300,14 @@ export class OAuthController extends MembershipBaseController {
288300
apis: []
289301
};
290302

303+
// Load permissions for all APIs
304+
const permissionData = await this.repos.rolePermission.loadUserPermissionInChurch(user.id, church.id);
305+
if (permissionData) {
306+
loginUserChurch.apis = permissionData.apis;
307+
}
308+
291309
// Create access token
292-
const accessToken = AuthenticatedUser.getChurchJwt(user, loginUserChurch);
310+
const accessToken = AuthenticatedUser.getCombinedApiJwt(user, loginUserChurch);
293311
const refreshToken = UniqueIdHelper.shortId();
294312

295313
// Store the refresh token for later use

0 commit comments

Comments
 (0)