我正在尝试通过 API 调用令牌在 Openshift 中使用它,以便我可以进行身份验证/授权并执行更多 API 调用
a)作为 URL,我使用“https://oauth-openshift.apps.domain/oauth/authorize?response_type=code&client_id=openshift-browser-client
b)我使用了基本身份验证用户/密码
c) 我使用的唯一标头是 X-CSRF-Token: xxx
我收到响应 200,但未显示令牌。它只是说“显示令牌”,但看不到它
我尝试更改response_type=token, client_id=openshift-challenging-client,但不起作用。
如何在邮递员中获取令牌以保存它并在下一个 API 调用中使用它(例如获取 pod、项目等)?
当我从跳转服务器使用curl(response_type = token,client_id = openshift-challenging-client)时,我得到了令牌,但我的目标是在邮递员中执行此操作
有效的卷曲示例 curl -v --insecure --user 用户:密码 --header "X-CSRF-Token: xxx" --url "https://oauth-openshift.apps.domain/oauth/authorize?response_type=token&client_id=openshift-挑战客户”2>&1 | grep -oP "access_token=\K[^&]*"
Postman 将 Postman 环境中的
access_token
变量的值设置为存储在 Access Token 变量中的值。
postman.setEnvironmentVariable("access_token", accessToken);
这会将 access_token 设置为 Bearer Token,允许使用占位符 {{access_token}} 访问它。然后可以在后续请求中动态使用此令牌以进行身份验证。
Access Token
Input : HTML
<form>
<input type="hidden" name="csrf" value="abc123token">
<button type="submit">
ee7fdbd598bf649359115e05b9a4e476a85ec6d9bc3c99af39476e66bb1db25c
</button>
</form>
解析
var accessToken = htmlResponse.match(/<button type="submit">([^<]+)<\/button>/)[1].trim();
通过“环境变量”设置/获取
postman.setEnvironmentVariable("access_token", accessToken);
console.log(postman.getEnvironmentVariable("access_token"));
另存为
server.js
const express = require('express');
const cors = require('cors');
const crypto = require('crypto');
let accessToken; // Variable to store the access token
const app = express();
app.use(cors()); // Enable CORS
// Get Token Endpoint
app.get('/oauth/authorize', (req, res) => {
// Check if Authorization header is present
if (!req.headers.authorization || req.headers.authorization.indexOf('Basic ') === -1) {
res.status(401).send('<h1>Unauthorized</h1>');
return;
}
// Extract the base64 encoded credentials
const base64Credentials = req.headers.authorization.split(' ')[1];
// Decode the base64 encoded credentials
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii');
// Extract username and password from credentials
const [username, password] = credentials.split(':');
// Check if username and password are valid
if (username !== 'abcd' || password !== '1234') {
res.status(401).send('<h1>Unauthorized</h1>');
return;
}
const responseType = req.query.response_type;
const csrfToken = req.headers['x-csrf-token'];
const clientId = extractClientId(csrfToken);
// Hash the CSRF token
accessToken = hashToken(csrfToken);
// Construct HTML response
const htmlResponse = `
<form>
<input type="hidden" name="csrf" value="${csrfToken}">
<button type="submit">
${accessToken}
</button>
</form>
`;
res.send(htmlResponse);
});
// Update data API End point
app.put('/data', (req, res) => {
const bearerToken = req.headers.authorization;
// Check if Authorization header is present and contains Bearer token
if (!bearerToken || bearerToken.indexOf('Bearer ') === -1) {
res.status(401).json({ error: 'Unauthorized: Bearer token missing' });
return;
}
const accessTokenFromHeader = bearerToken.split('Bearer ')[1]; // Extract the Bearer token
// Check if the access token matches the expected value
if (accessTokenFromHeader !== accessToken) {
res.status(403).json({ error: 'Forbidden: Invalid access token' });
return;
}
const updatedData = { message: "Data updated successfully" };
res.json(updatedData); // Send the updated data in JSON format
});
// Function to extract client ID from CSRF token
function extractClientId(csrfToken) {
// For simplicity, let's assume the CSRF token contains the client ID at the end of the token.
return csrfToken.slice(-20); // Extract last 20 characters as client ID
}
function hashToken(token) {
return crypto.createHash('sha256').update(token).digest('hex');
}
// Start the server
const port = 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
npm install express cors crypto
node server.js
用户ID:用户 用户密码:1234
curl \
--silent \
--insecure \
--user abcd:1234 \
--header "X-CSRF-Token: abc123token" \
--url "http://localhost:3000/oauth/authorize?response_type=token&client_id=openshift-challenging-client"
输出将响应 HTML 格式,与您的响应类似
GET http://localhost:3000/oauth/authorize?response_type=token&client_id=openshift-challenging-client
Header
Tests
var htmlResponse = pm.response.text();
var accessToken = htmlResponse.match(/<button type="submit">([^<]+)<\/button>/)[1].trim();
postman.setEnvironmentVariable("access_token", accessToken);
console.log(postman.getEnvironmentVariable("access_token"));
PUT http://localhost:3000/data
选择带有“承载令牌
and Enter
{{access_token}}”的类型
它将使用一些环境变量的名称(我的演示是“dev”)
Pre-request Script
刚刚记录