Example: identify requester to protected endpoints

General steps

  1. 1.
    Get application_key from request Authorization header or from session cookie
  2. 2.
    Pass application_key to context.get_user_by_application_key method
Server-side (Flask)
Server-side (Node)
Client-side (React)
1
from flask import Flask, request
2
from wayscript import context
3
4
app = Flask(__name__)
5
6
@app.route('/')
7
def get_user_details():
8
# Parse application key from bearer token in request header
9
application_key = request.headers.get('Authorization')[7:]
10
# Query user object from application key
11
user = context.get_user_by_application_key(application_key)
12
return user
13
14
if __name__ == '__main__':
15
app.run()
Copied!
1
const express = require('express');
2
const wayscript = require("wayscript");
3
4
const app = express();
5
const port = 3000;
6
7
app.get('/', (req, res) => {
8
// Parse application key from bearer token in request header
9
const applicationKey = req.headers.authorization.substr(7);
10
// Query user object from application key
11
let user = wayscript.context.getUserByApplicationKey(applicationKey);
12
res.send(user);
13
});
14
15
app.listen(port, () => console.log(`Hello world app listening on port ${port}!`));
Copied!
WayScript also sets the application_key as the value for a cookie with name ws_workspace_application_key_<workspace_id> for client-side processing. You can determine your workspace_id by inspecting cookies for a protected endpoint.
1
import React from 'react';
2
import Cookies from 'js-cookie';
3
const wayscript = require("wayscript");
4
5
class App extends React.Component {
6
// Retrieve application key from session cookie
7
const applicationKey = Cookies.get('ws_workspace_application_key_<workspace_id>');
8
// Query user object from application key
9
const user = wayscript.context.getUserByApplicationKey(state.applicationKey);
10
}
Copied!

Sample user metadata

1
{
2
"avatar": "https://lh3.googleusercontent.com/a-/AOh14Gj_lywgCvZF1oDt0Z0iW3n01MtyyYA0YTRYxlxq=s96-c",
3
"created_date": "2022-01-27T19:31:11.719832",
4
"email": "[email protected]",
5
"first_name": "Nihar",
6
"last_name": "Parikh",
7
"id": "186f48b6-57c5-4dd4-9422-f7d503ad5b9c",
8
"groups": [
9
{
10
"id": "195d0f07-1b9f-4c7e-ae48-227dcf63556a",
11
"name": "product_team",
12
"owner_id": "186f48b6-57c5-4dd4-9422-f7d503ad5b9c",
13
"system_managed": false,
14
"workspace_id": "40dea282-d83b-48d1-9427-f01171da45f6"
15
}
16
],
17
}
Copied!
Export as PDF
Copy link