|
| 1 | +// https://developer.linkedin.com/docs/oauth2 |
| 2 | + |
| 3 | +var OAuth = require('oauth').OAuth2 |
| 4 | +var HTML = require('./html') |
| 5 | +var superagent = require('superagent') |
| 6 | + |
| 7 | +const ClaimType = 9 // Has LinkedIn |
| 8 | + |
| 9 | +module.exports = function facebook(app, { web3, linkedInApp, baseUrl }) { |
| 10 | + const redirect_uri = `${baseUrl}/linkedin-auth-response` |
| 11 | + |
| 12 | + var linkedInOAuth = new OAuth( |
| 13 | + linkedInApp.client_id, |
| 14 | + linkedInApp.secret, |
| 15 | + 'https://www.linkedin.com', |
| 16 | + '/oauth/v2/authorization', |
| 17 | + '/oauth/v2/accessToken', |
| 18 | + null |
| 19 | + ) |
| 20 | + |
| 21 | + app.get('/linkedin-auth', (req, res) => { |
| 22 | + if (!req.query.target) { |
| 23 | + res.send('No target identity contract provided') |
| 24 | + return |
| 25 | + } |
| 26 | + if (!req.query.issuer) { |
| 27 | + res.send('No issuer identity contract provided') |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + req.session.targetIdentity = req.query.target |
| 32 | + req.session.issuer = req.query.issuer |
| 33 | + req.session.state = web3.utils.randomHex(8) |
| 34 | + |
| 35 | + var authURL = linkedInOAuth.getAuthorizeUrl({ |
| 36 | + redirect_uri, |
| 37 | + scope: ['r_basicprofile', 'r_emailaddress'], |
| 38 | + state: req.session.state, |
| 39 | + response_type: 'code' |
| 40 | + }) |
| 41 | + |
| 42 | + res.redirect(authURL) |
| 43 | + }) |
| 44 | + |
| 45 | + app.get( |
| 46 | + '/linkedin-auth-response', |
| 47 | + (req, res, next) => { |
| 48 | + linkedInOAuth.getOAuthAccessToken( |
| 49 | + req.query.code, |
| 50 | + { |
| 51 | + redirect_uri, |
| 52 | + grant_type: 'authorization_code' |
| 53 | + }, |
| 54 | + function(e, access_token, refresh_token, results) { |
| 55 | + if (e) { |
| 56 | + next(e) |
| 57 | + } else if (results.error) { |
| 58 | + next(results.error) |
| 59 | + } else { |
| 60 | + req.access_token = access_token |
| 61 | + next() |
| 62 | + } |
| 63 | + } |
| 64 | + ) |
| 65 | + }, |
| 66 | + (req, res, next) => { |
| 67 | + superagent |
| 68 | + .get('https://api.linkedin.com/v1/people/~') |
| 69 | + .set('Authorization', `Bearer ${req.access_token}`) |
| 70 | + .query({ format: 'json' }) |
| 71 | + .then(response => { |
| 72 | + req.linkedInUser = response.body |
| 73 | + next() |
| 74 | + }) |
| 75 | + }, |
| 76 | + async (req, res) => { |
| 77 | + // var data = JSON.stringify({ user_id: req.githubUser.id }) |
| 78 | + var rawData = 'Verified OK' |
| 79 | + var hashedData = web3.utils.soliditySha3(rawData) |
| 80 | + var hashed = web3.utils.soliditySha3( |
| 81 | + req.session.targetIdentity, |
| 82 | + ClaimType, |
| 83 | + hashedData |
| 84 | + ) |
| 85 | + req.signedData = await web3.eth.accounts.sign( |
| 86 | + hashed, |
| 87 | + linkedInApp.claimSignerKey |
| 88 | + ) |
| 89 | + |
| 90 | + res.send( |
| 91 | + HTML(` |
| 92 | + <div class="mb-2">Successfully signed claim:</div> |
| 93 | + <div class="mb-2"><b>Issuer:</b> ${req.session.issuer}</div> |
| 94 | + <div class="mb-2"><b>Target:</b> ${req.session.targetIdentity}</div> |
| 95 | + <div class="mb-2"><b>Data:</b> ${rawData}</div> |
| 96 | + <div class="mb-2"><b>Signature:</b> ${req.signedData.signature}</div> |
| 97 | + <div class="mb-2"><b>Hash:</b> ${req.signedData.messageHash}</div> |
| 98 | + <div><button class="btn btn-primary" onclick="window.done()">OK</button></div> |
| 99 | + <script> |
| 100 | + window.done = function() { |
| 101 | + window.opener.postMessage('signed-data:${ |
| 102 | + req.signedData.signature |
| 103 | + }:${req.signedData.messageHash}:${ClaimType}', '*') |
| 104 | + } |
| 105 | + </script>`) |
| 106 | + ) |
| 107 | + } |
| 108 | + ) |
| 109 | +} |
0 commit comments