Skip to content

Commit

Permalink
fix: remove danger type cast
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Jul 27, 2024
1 parent 3f033e2 commit 54a96c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
1 change: 0 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { accounts } from './pkg/accounts/mod.js';
import { drive } from './pkg/drive/mod.js';
import { noteHandlers } from './pkg/notes/mod.js';
import { timeline } from './pkg/timeline/mod.js';
import { swaggerUI } from '@hono/swagger-ui';

export const app = new Hono().get('/doc', async (c) => {
// NOTE: If you create a new module, you must add module API doc base path here.
Expand Down
33 changes: 26 additions & 7 deletions pkg/adaptors/authenticateMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,43 @@ export type AuthMiddlewareVariable = {
accountName: Option.Option<string>;
};

type tokenPayload = { sub: string; accountName: string };

export class AuthenticateMiddlewareService {
private readonly authTokenService: AuthenticationTokenService;

constructor(authTokenService: AuthenticationTokenService) {
this.authTokenService = authTokenService;
}

private parseToken(
token: string,
): Option.Option<{ sub: string; accountName: string }> {
private parseToken(token: string): Option.Option<tokenPayload> {
const split = token.split('.')[1];
if (!split) {
return Option.none();
}
const payload = JSON.parse(
Buffer.from(split, 'base64').toString('utf-8'),
) as { sub: string; accountName: string };
return Option.some({ sub: payload.sub, accountName: payload.accountName });
const payload = JSON.parse(Buffer.from(split, 'base64').toString('utf-8'));

const isPayloadValid = (p: object): p is tokenPayload => {
if (!('sub' in p) || !('accountName' in p)) {
return false;
}
if (
typeof payload.sub !== 'string' ||
typeof payload.accountName !== 'string'
) {
return false;
}
return true;
};

if (!isPayloadValid(payload)) {
return Option.none();
}

return Option.some({
sub: payload.sub,
accountName: payload.accountName,
});
}

/**
Expand Down

0 comments on commit 54a96c6

Please sign in to comment.