criipto 验证- 表达式
接受MitID、NemID、瑞典BankID、挪威BankID以及更多使用Passport或plain Express的Node.js应用程序登录
安装
使用 无
npm install @criipto/verify-express
开始
您可以在您的域名和应用程序客户端 ID 上找到 Criipto 盘。 。 。 .
如果您没有您的客户端秘密存储在任何地方, 请确保启用代码流和/或重现您的客户端秘密 .
以下为单页应用程序和Web应用程序使用例。 你也可以这样 从 GitHub 下载完整样本
调试
DEBUG=@criipto/verify-express*
带会话的网络应用并重定向.
在使用基于方向的认证时必须设置会话 .
您还必须在您的 Criipto 应用程序上注册一个符合您方向处理路径的 URL 的 Callback URL .
// server.js
const express = require('express');
const expressSesssion = require('express-session');
const app = express();
app.use(
expressSesssion({
secret: '{{YOUR_SESSION_SECRET}}',
resave: false,
saveUninitialized: true
})
);
护照
// server.js
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
const redirectPassport = new CriiptoVerifyRedirectPassportStrategy(
{
domain: "{{YOUR_CRIIPTO_DOMAIN}}",
clientID: "{{YOUR_CLIENT_ID}}",
clientSecret: CRIIPTO_CLIENT_SECRET,
// Should match an express route that is an allowed callback URL in your application
// This route should also have the authentication middleware applied.
redirectUri: '/login',
postLogoutRedirectUri: '/',
// Ammend authorize request if you wish
beforeAuthorize(req, options) {
return {
...options,
acr_values: req.query.acr_values,
prompt: req.query.prompt
}
}
},
// Map claims to an express user
async (jwtClaims) => {
return jwtClaims;
}
);
// Route to both trigger and handle redirect
app.get('/login', passport.authenticate('criiptoVerifyRedirect', {failureRedirect: '/error', successReturnToOrRedirect: '/passport/protected'}), (req, res) => {
res.json(req.user);
});
app.get('/protected', passport.authenticate('criiptoVerifyRedirect', {}), (req, res) => {
res.json(req.user);
});
app.get('/logout', redirectPassport.logout.bind(redirectPassport));
app.get('/error', function (req, res, next) {
res.json({
error: req.query.error,
error_description: req.query.error_description,
});
});
普通快递
const expressRedirect = new CriiptoVerifyExpressRedirect({
domain: "{{YOUR_CRIIPTO_DOMAIN}}",
clientID: "{{YOUR_CLIENT_ID}}",
clientSecret: CRIIPTO_CLIENT_SECRET,
// Should match an express route that is an allowed callback URL in your application
// This route should also have the authentication middleware applied.
redirectUri: '/login',
postLogoutRedirectUri: '/',
// Ammend authorize request if you wish
beforeAuthorize(req, options) {
return {
...options,
acr_values: req.query.acr_values,
prompt: req.query.prompt
}
}
});
// Route to both trigger and handle redirect
app.get('/login', expressRedirect.middleware({failureRedirect: '/error', successReturnToOrRedirect: '/plain/protected'}), (req, res) => {
res.json(req.claims);
});
app.get('/protected', expressRedirect.middleware({}), (req, res) => {
res.json(req.claims);
});
app.get('/logout', expressRedirect.logout.bind(expressRedirect));
app.get('/error', function (req, res, next) {
res.json({
error: req.query.error,
error_description: req.query.error_description,
});
});
单页应用程序
SPA 可以利用前端框架,例如 校对:soup 或者说 @criipto/验证-反应 用于处理前端的登录,然后向它们的API发送一个 Bearer 令牌.
您必须在匹配 Criipto 应用程序的 Criipto 中注册回调 URL href 您正在触发 SPA 登录的 URL .
护照
// server.js
const express = require('express');
const passport = require('passport');
const CriiptoVerifyJwtPassportStrategy = require('@criipto/verify-express').CriiptoVerifyJwtPassportStrategy;
const app = express();
app.use(passport.initialize());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use(
'criiptoVerifyJwt',
new CriiptoVerifyJwtPassportStrategy({
domain: "{{YOUR_CRIIPTO_DOMAIN}}",
clientID: "{{YOUR_CLIENT_ID}}"
},
// Map claims to an express user
async (jwtClaims) => {
return jwtClaims;
})
);
app.get('/jwt-protected-route', passport.authenticate('criiptoVerifyJwt', { session: false }), (req, res) => {
res.json({
...req.user,
passport: 'says hi'
});
});
// client.js
const {id_token} = login();
fetch(`{server}/jwt-protected-route`, {
headers: {
Authorization: `Bearer ${id_token}`
}
})
普通快递
// server.js
const express = require('express');
const CriiptoVerifyExpressJwt = require('@criipto/verify-express').CriiptoVerifyExpressJwt;
const app = express();
const expressJwt = new CriiptoVerifyExpressJwt({
domain: "{{YOUR_CRIIPTO_DOMAIN}}",
clientID: "{{YOUR_CLIENT_ID}}"
});
app.get('/jwt-protected-route', expressJwt.middleware(), (req, res) => {
res.json({
...req.user,
express: 'says hi'
});
});
// client.js
const {id_token} = login();
fetch(`{server}/jwt-protected-route`, {
headers: {
Authorization: `Bearer ${id_token}`
}
})