Skip to content

Commit

Permalink
Assign admin panel s2s app with root scope for simpler migration (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
byn9826 authored Aug 1, 2024
1 parent 7a3a2e2 commit 832c835
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 11 deletions.
2 changes: 1 addition & 1 deletion admin-panel/app/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const obtainS2SAccessToken = async () => {

const body = {
grant_type: 'client_credentials',
scope: `${Scope.ReadUser} ${Scope.WriteUser} ${Scope.ReadApp} ${Scope.WriteApp} ${Scope.ReadRole} ${Scope.WriteRole} ${Scope.ReadScope} ${Scope.WriteScope}`,
scope: Scope.Root,
}
const res = await fetch(
`${process.env.NEXT_PUBLIC_SERVER_URI}/oauth2/v1/token`,
Expand Down
2 changes: 1 addition & 1 deletion docs/admin-panel.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ npm run dev
cd melody-auth/server
wrangler d1 execute melody-auth --command="insert into user_role (userId, roleId) values (1, 1)"
```
4. Refresh the admin panel page. You should now have full access.
4. Logout and login again. You should now have full access.
1 change: 1 addition & 0 deletions server/migrations/0006_create_scope_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CREATE UNIQUE INDEX idx_unique_scope_name ON scope (name) WHERE deletedAt IS NUL
INSERT INTO scope ("name", "type") values ("openid", "spa");
INSERT INTO scope ("name", "type") values ("profile", "spa");
INSERT INTO scope ("name", "type") values ("offline_access", "spa");
INSERT INTO scope ("name", "type") values ("root", "s2s");
INSERT INTO scope ("name", "type") values ("read_user", "s2s");
INSERT INTO scope ("name", "type") values ("write_user", "s2s");
INSERT INTO scope ("name", "type") values ("read_app", "s2s");
Expand Down
9 changes: 1 addition & 8 deletions server/migrations/0007_create_app_scope_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,4 @@ CREATE UNIQUE INDEX idx_unique_app_scope ON app_scope (appId, scopeId) WHERE del
INSERT INTO app_scope ("appId", "scopeId") values (1, 1);
INSERT INTO app_scope ("appId", "scopeId") values (1, 2);
INSERT INTO app_scope ("appId", "scopeId") values (1, 3);
INSERT INTO app_scope ("appId", "scopeId") values (2, 4);
INSERT INTO app_scope ("appId", "scopeId") values (2, 5);
INSERT INTO app_scope ("appId", "scopeId") values (2, 6);
INSERT INTO app_scope ("appId", "scopeId") values (2, 7);
INSERT INTO app_scope ("appId", "scopeId") values (2, 8);
INSERT INTO app_scope ("appId", "scopeId") values (2, 9);
INSERT INTO app_scope ("appId", "scopeId") values (2, 10);
INSERT INTO app_scope ("appId", "scopeId") values (2, 11);
INSERT INTO app_scope ("appId", "scopeId") values (2, 4);
3 changes: 2 additions & 1 deletion server/src/middlewares/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ const s2sScopeGuard = async (
if (!accessTokenBody) return false

const scopes = accessTokenBody.scope?.split(' ') ?? []
if (!scopes.includes(scope)) return false

if (!scopes.includes(scope) && !scopes.includes(Scope.Root)) return false

c.set(
'access_token_body',
Expand Down
1 change: 1 addition & 0 deletions shared/src/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum Scope {
OpenId = 'openid',
Profile = 'profile',
OfflineAccess = 'offline_access',
Root = 'root',
ReadUser = 'read_user',
WriteUser = 'write_user',
ReadApp = 'read_app',
Expand Down

0 comments on commit 832c835

Please sign in to comment.