Sync SSO node.js implementation

Here is example with NodeJS if anyone needs it:

'use strict';

const axios = require('axios');
const crypto = require('crypto');
const qs = require('querystring');
const config = require('./config');

function getHash(data, secret) {
  return crypto.createHmac('sha256', secret)
    .update(data)
    .digest('hex');
}

function syncSso() {
  const endpoint = `${config.discourse.url}/admin/users/sync_sso`;
  const params = {
    external_id: 1,
    email: 'bob@example.com',
    username: 'bob',
    add_groups: 'eurorack',
    require_activation: true
  };
  const headers = {
    'Api-Key': config.discourse.apiKey,
    'Api-Username':  config.discourse.apiUsername,
    'Content-Type': 'multipart/form-data'
  };
  const sso = Buffer.from(qs.stringify(params)).toString('base64');
  const sig = getHash(sso, config.discourse.ssoSecret);
  return axios.post(endpoint, qs.stringify({ sso, sig }), { headers })
    .then(({ data }) => data)
    .catch(err => err);
}
4 Likes