[英]Auth0 "The JWT string must contain two dots"
我目前正在使用 Vue3 並集成了來自https://github.com/auth0/auth0-spa-js的 Auth0-spa-js。 這很好用。
我正在通過 Axios 向 PHP API 后端發送請求,將訪問令牌作為稱為令牌的 GET 參數傳遞。
服務器端在設置來自https://github.com/auth0/auth0-PHP的步驟后,我得到一個異常“JWT 字符串必須包含兩個點”。 我已經安裝了要求、guzzle 和 dotenv 等。目前在 PHP 7.4.2。
// useAuth0.js
// to login and maintain Auth state
import createAuth0Client from "@auth0/auth0-spa-js";
import { reactive } from "vue";
export const AuthState = reactive({
user: null,
loading: false,
isAuthenticated: null,
auth0: null,
});
const config = {
domain: import.meta.env.VITE_AUTH0_DOMAIN,
client_id: import.meta.env.VITE_AUTH0_CLIENT_ID,
};
export const useAuth0 = (state) => {
const handleStateChange = async () => {
state.isAuthenticated = !!(await state.auth0.isAuthenticated());
state.user = await state.auth0.getUser();
state.loading = false;
};
const initAuth = () => {
state.loading = true;
createAuth0Client({
domain: config.domain,
client_id: config.client_id,
cacheLocation: "localstorage",
redirect_uri: window.location.origin,
}).then(async (auth) => {
state.auth0 = auth;
await handleStateChange();
});
};
const login = async () => {
await state.auth0.loginWithPopup();
await handleStateChange();
};
const logout = async () => {
state.auth0.logout({
returnTo: window.location.origin,
});
};
return {
login,
logout,
initAuth,
};
};
// and I use this on a button click event
AuthState.auth0.getTokenSilently().then(accessToken => {
// AXIOS REQUEST
})
// PHP
// Auth0 SDK is 8.1.0
use Auth0\SDK\Auth0;
use Auth0\SDK\Utility\HttpResponse;
use Auth0\SDK\Token;
$env = (Dotenv\Dotenv::createImmutable(FCPATH))->load();
// I've checked that $env does contain correct .env values
$token = filter_var($_GET['token'] ?? null, FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
// Actual token I logged
eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIiwiaXNzIjoiaHR0cHM6Ly9kZXYtd2kxeGRtbDcudXMuYXV0aDAuY29tLyJ9..V50FRJnBnpBnHJjA.e3PZuESoGaPCjp0kO9vlijGMIfhXWQHlbvsslWtbAvFAQ5hef9_PXLD_W282Cba9D6k-FAwhro9i3e5ukzXouGWYfoYHHQ5WQJ-vpLISrRANxFvNVPsCZSkg1sAIbL0Qk3Gir82ds1G919uEPc6vB3Y2qbARAd9nlMJBpLqWUq9VcIrzHtsJN7Q8j36vTCRXyu0f5-TeOr-dU3-gaIUvur37YQD0xICr4sENFktPU3s-uqCSCopVi6MoZMGvfYcVlO3nv1Sb2owGX_S_PSG7fug4Et-pMw1cVYgfNtLQf8XViI-l19sgXAf2eQShmLPvcdBdXVPA0g.S9vyktmK7rPoM_F3nUSEvg
$auth0 = new Auth0([
'domain' => $env['AUTH0_DOMAIN'],
'clientId' => $env['AUTH0_CLIENT_ID'],
'clientSecret' => $env['AUTH0_CLIENT_SECRET'],
'tokenAlgorithm' => 'RS256'
]);
// Exception thrown here with decode
$token = $auth0->decode($token, null, null, null, null, null, null, Token::TYPE_ID_TOKEN);
$token->verify();
$token->validate();
在創建與 Auth0 PHP SDK 不兼容的令牌時,auth0-spa-js 是否存在問題,或者未傳遞我需要添加的配置設置? 我已經按照這兩個文檔的說明配置了很多東西,仔細檢查了預期的變量。
結果我需要將受眾參數添加到 createAuth0Client、getTokenSilently() 和 PHP SDK 解碼方法,用於我的 Auth0 自定義 API。一切都經過驗證。
我一定是錯過了文檔中的某些內容,或者 audience 參數似乎比可選值更像是必需的。
添加到Brian Barton 上面的回答, GetTokenSilentlyOptions
接口解釋說選項應該按如下方式傳遞:
// ✅ Like this!
const token = await this.$auth.getTokenSilently({
authorizationParams: {
audience: 'https://api.example.com/',
},
})
// ❌ NOT like this (where I got stuck for a while)
const token = await this.$auth.getTokenSilently({
audience: 'https://api.example.com/',
})
額外的外部 object 結構對我來說不是很明顯,所以我無法弄清楚為什么我不能讓他們的解決方案工作。
這與您在服務器端擁有的內容無關,也與您的 SPA 如何按照帶有代碼交換證明密鑰的授權代碼流 (PKCE)檢索您的 884645589888 令牌有關(如果您'正在從 SPA 訪問 API)。
我最終找到了這個答案,因為當我未能正確設置audience
參數時,Auth0 沒有發出任何錯誤的信號。 它返回了一個不是格式正確的 JWT 的“令牌”。在我的例子中,它始終有四個或五個點(句點),而本應恰好有兩個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.