Skip to content

Commit

Permalink
refactor(sara_token.js): drop sara header, use iss claim instead
Browse files Browse the repository at this point in the history
  • Loading branch information
supersonictw committed Oct 13, 2024
1 parent 52ce924 commit b8edcb8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
1 change: 0 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ MAIL_SMTP_PASSWORD="your_smtp_password"
MAIL_SMTP_FROM='"sara.recv" <[email protected]>'

# sara configs
SARA_ISSUER="https://example.com"
SARA_AUDIENCE_URL="https://example.org"
SARA_SYSTEM_ADMIN_SECRET="your_system_admin_secret"
SARA_SYSTEM_ADMIN_IP_ADDRESS="127.0.0.1"
22 changes: 6 additions & 16 deletions src/utils/sara_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@ const {sign, verify} = require("jsonwebtoken");
// Import usePublicKey and usePrivateKey
const {usePublicKey, usePrivateKey} = require("../init/keypair");

// Define Sara Token specs
const issuerIdentity = "Sara Hoshikawa"; // The code of Sara v3

// Define issueOptions
const issueOptions = {
algorithm: "ES256",
expiresIn: "1d",
notBefore: "500ms",
issuer: getMust("SARA_ISSUER"),
issuer: issuerIdentity,
audience: getMust("SARA_AUDIENCE_URL"),
noTimestamp: false,
mutatePayload: false,
header: {
sara: {
version: 3,
type: "auth",
},
},
};

// Define validateOptions
const validateOptions = {
algorithms: ["ES256"],
issuer: getMust("SARA_ISSUER"),
issuer: issuerIdentity,
audience: getMust("SARA_AUDIENCE_URL"),
complete: true,
};
Expand Down Expand Up @@ -62,17 +59,10 @@ function validate(token) {
};

try {
const {header, payload} = verify(
const {payload} = verify(
token, publicKey, validateOptions,
);

if (
header?.sara?.version !== 3 ||
header?.sara?.type !== "auth"
) {
throw new Error("invalid sara token type");
}

result.userId = payload.sub;
result.payload = payload;
} catch (e) {
Expand Down

0 comments on commit b8edcb8

Please sign in to comment.