|
| 1 | +--- |
| 2 | +slug: grant-oauth-proxy-with-crossid |
| 3 | +title: Using grant OAuth2 proxy with Crossid |
| 4 | +description: How to use grant OAuth2 nodejs proxy with Crossid. |
| 5 | +author: Asaf Shakarzy |
| 6 | +author_title: Core Team |
| 7 | +author_url: https://github.com/asaf |
| 8 | +tags: [oauth2, openid, nodejs, proxy] |
| 9 | +--- |
| 10 | + |
| 11 | +import AddIntegration from "../src/components/AddIntegration.js"; |
| 12 | +import AppUserAssignment from "../src/components/AssignUserToApp.mdx"; |
| 13 | + |
| 14 | +[grant](https://github.com/simov/grant) is a nodejs OAuth proxy. |
| 15 | + |
| 16 | +With few lines of code, _grant_ can add oauth login support for any node.js app. |
| 17 | + |
| 18 | +For example, [Strapi](https://strapi.io/) uses _grant_ to authenticate users via Oauth2. |
| 19 | + |
| 20 | +This post explains how to use _grant_ to authenticate Crossid. |
| 21 | + |
| 22 | +## Init a new project |
| 23 | + |
| 24 | +```bash |
| 25 | +mkdir grant-crossid && cd grant-crossid |
| 26 | +npm -y init |
| 27 | +npm install grant express express-session |
| 28 | +``` |
| 29 | + |
| 30 | +## Let crossid know about Grant |
| 31 | + |
| 32 | +We need to let Crossid know about our grant app. |
| 33 | + |
| 34 | +<AddIntegration name="Web App" steps={["Redirect URIs: http://localhost:3005/connect/crossid/callback"]}/> |
| 35 | + |
| 36 | +### Grant your user access to this app. |
| 37 | + |
| 38 | +<AppUserAssignment/> |
| 39 | + |
| 40 | +Save the _Client ID_ to the next deployment steps. |
| 41 | + |
| 42 | +## config.json |
| 43 | + |
| 44 | +```json {8-10} |
| 45 | +{ |
| 46 | + "defaults": { |
| 47 | + "origin": "http://localhost:3005", |
| 48 | + "transport": "session", |
| 49 | + "state": true |
| 50 | + }, |
| 51 | + "crossid": { |
| 52 | + "subdomain": "acme", |
| 53 | + "key": "<client_id>", |
| 54 | + "secret": "<client_secret>", |
| 55 | + "scope": ["openid"], |
| 56 | + "callback": "/hello", |
| 57 | + "response": ["tokens", "raw", "profile"] |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +The configuration file defines the _crossid_ as an auth provider. |
| 63 | + |
| 64 | +- replace _subdomain_ with your [tenant](/docs/concepts/tenant). |
| 65 | +- replace _<client_id>_ and _<client_secret>_ with your app credentials. |
| 66 | + |
| 67 | +## index.js |
| 68 | + |
| 69 | +```js |
| 70 | +var express = require("express"); |
| 71 | +var session = require("express-session"); |
| 72 | +var grant = require("grant").express(); |
| 73 | + |
| 74 | +express() |
| 75 | + .use(session({ secret: "grant", saveUninitialized: true, resave: false })) |
| 76 | + .use(grant(require("./config.json"))) |
| 77 | + .get("/hello", (req, res) => { |
| 78 | + res.end(JSON.stringify(req.session.grant.response, null, 2)); |
| 79 | + }) |
| 80 | + .listen(3005); |
| 81 | +``` |
| 82 | + |
| 83 | +## Try it |
| 84 | + |
| 85 | +Run the example by: `node index.js` |
| 86 | + |
| 87 | +Open browser in _https://localhost:3005/connect/crossid_ |
| 88 | + |
| 89 | +You should be redirected to the login page, once user is logged in, you should see the _access token_ and the user's profile. |
| 90 | + |
| 91 | +## References |
| 92 | + |
| 93 | +- repo: https://github.com/simov/grant |
0 commit comments