29 lines
1.1 KiB
JavaScript
29 lines
1.1 KiB
JavaScript
import { getHashString } from './utils.js'
|
|
|
|
export async function getAccessToken(subid, timestamp, secretBase) {
|
|
return await getHashString(`${subid}${timestamp}${secretBase}`);
|
|
}
|
|
|
|
export async function verifyAccessToken(request, env, ctx) {
|
|
const headers = request.headers;
|
|
const subid = headers.get('x-subject-id');
|
|
const timestamp = headers.get('x-timestamp');
|
|
const token = headers.get('x-access-token');
|
|
const secretBase = env.ACCESS_TOKEN_BASE;
|
|
return token === await getAccessToken(subid, timestamp, secretBase);
|
|
}
|
|
|
|
export async function getAccessTokenDispatcher(request, env, ctx) {
|
|
const subid = request.headers.get('x-subject-id');
|
|
if (subid === null)
|
|
return new Response(null, { status: 400 });
|
|
|
|
const timestamp = new Date().toISOString().replace(/:/g, '_');
|
|
const secretBase = env.ACCESS_TOKEN_BASE;
|
|
|
|
const token = await getAccessToken(subid, timestamp, secretBase);
|
|
const res = JSON.stringify({ token, timestamp });
|
|
return new Response(res, { status: 200 });
|
|
}
|
|
|
|
export default getAccessTokenDispatcher; |