Skip to content

Commit

Permalink
noddy basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbuckley committed Apr 12, 2024
1 parent 03154a0 commit 42ed276
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .github/workflows/cloudflare-worker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ jobs:
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
workingDirectory: .
secrets: |
BASIC_AUTH
SLACK_TOKEN
env:
BASIC_AUTH: ${{ secrets.BASIC_AUTH }}
SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@
// is everything in the event or do we need a separate info call?
// Get-Employee header field if needed

import { Buffer } from 'node:buffer';
import Slack from './slack';

function authorize(request, env) {
const authHeader = request.headers.get('Authorization');
if (!authHeader) { return false; }

const [scheme, auth] = authHeader.split(' ');
if (scheme != 'Basic' || !auth) { return false; }

return (env.BASIC_AUTH == Buffer.from(auth, 'base64').toString());
}

export default {
async fetch(request, env, ctx) {
const requestUrl = new URL(request.url);
const slack = new Slack(env.SLACK_TOKEN);

if (!authorize(request, env)) {
return new Response('Not authed', {
status: 401,
headers: {'www-authenticate': 'Basic realm="bob-slack-integration", charset="utf-8"'},
});
}

const userId = await slack.getUserId(requestUrl.searchParams.get('email'));

if (!userId) {
return new Response('User not found', {status: 404});
}

return await slack.setUserProfile(userId, {
capability: 'Architecture',
contractedLocation: 'Leeds',
Expand Down
1 change: 1 addition & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name = "bob-slack-integration"
main = "index.js"
compatibility_date = "2024-04-11"
node_compat = true

0 comments on commit 42ed276

Please sign in to comment.