|
| 1 | +import {HttpRequest, HttpResponse} from 'uWebSockets.js' |
| 2 | +import uWSAsyncHandler from '../graphql/uWSAsyncHandler' |
| 3 | +import parseBody from '../parseBody' |
| 4 | +import querystring from 'querystring' |
| 5 | +import publishWebhookGQL from '../utils/publishWebhookGQL' |
| 6 | +import SCIMMY from 'scimmy' |
| 7 | + |
| 8 | +const ROUTE_PREFIX = '/scim' |
| 9 | +const PROTO = process.env.PROTO |
| 10 | +const HOST = process.env.HOST |
| 11 | +const PORT = Number(__PRODUCTION__ ? process.env.PORT : process.env.SOCKET_PORT) |
| 12 | + |
| 13 | +const ROUTE = `${PROTO}://${HOST}${ROUTE_PREFIX}` |
| 14 | + |
| 15 | +const createUser = ` |
| 16 | +mutation ScimCreateUser($email: ID!, $preferredName: String!) { |
| 17 | + scimCreateUser(email: $email, preferredName: $preferredName) { |
| 18 | + ... on ErrorPayload { |
| 19 | + error { |
| 20 | + message |
| 21 | + } |
| 22 | + } |
| 23 | + ...on ScimCreateUserSuccess { |
| 24 | + user { |
| 25 | + id |
| 26 | + email |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | +` |
| 32 | + |
| 33 | +const getUser = ` |
| 34 | +query User($userId: ID!) { |
| 35 | + user(userId: $userId) { |
| 36 | + id |
| 37 | + email |
| 38 | + } |
| 39 | +} |
| 40 | +` |
| 41 | + |
| 42 | +// With both shorthand and full syntax |
| 43 | +SCIMMY.Config.set({ |
| 44 | + //documentationUri: "https://example.com/docs/scim.html", |
| 45 | + patch: false, |
| 46 | + filter: false, |
| 47 | + bulk: false,/*{ |
| 48 | + supported: true, |
| 49 | + maxPayloadSize: 2097152 |
| 50 | + },*/ |
| 51 | + authenticationSchemes: { |
| 52 | + name: 'OAuth Bearer Token', |
| 53 | + description: 'Authentication scheme using the OAuth Bearer Token Standard', |
| 54 | + specUri: 'http://www.rfc-editor.org/info/rfc6750', |
| 55 | + type: 'oauthbearertoken' |
| 56 | + } |
| 57 | +}); |
| 58 | + |
| 59 | +// Basic usage with provided resource type implementations |
| 60 | +SCIMMY.Resources.declare(SCIMMY.Resources.User) |
| 61 | + .ingress(async (_resource, data) => { |
| 62 | + console.log('GEORG User ingress', data.id, data.userName) |
| 63 | + const { userName, displayName, emails } = data |
| 64 | + const email = (emails?.find((email) => email.primary) ?? emails?.[0])?.value |
| 65 | + console.log('GEORG User ingress', displayName, userName, email) |
| 66 | + |
| 67 | + const newUser = await publishWebhookGQL(createUser, { |
| 68 | + email, |
| 69 | + preferredName: displayName ?? userName |
| 70 | + }) |
| 71 | + console.log('GEORG User ingress newUser', newUser) |
| 72 | + const {id, email: normalizedEmail} = (newUser as any).data.scimCreateUser.user |
| 73 | + return { |
| 74 | + id, |
| 75 | + userName: normalizedEmail |
| 76 | + } |
| 77 | + }) |
| 78 | + .egress(async (_resource, userId) => { |
| 79 | + console.log('GEORG User egress', userId) |
| 80 | + const user = await publishWebhookGQL(getUser, {userId}) |
| 81 | + console.log('GEORG User egress', user) |
| 82 | + const {id, email} = (user as any).data.user |
| 83 | + console.log('GEORG User egress', id, email) |
| 84 | + return { |
| 85 | + id, |
| 86 | + userName: email |
| 87 | + } |
| 88 | + }) |
| 89 | + .degress((resource) => { |
| 90 | + console.log('GEORG User degress', resource) |
| 91 | + }); |
| 92 | + |
| 93 | +SCIMMY.Resources.Schema.basepath(ROUTE) |
| 94 | +SCIMMY.Resources.ResourceType.basepath(ROUTE) |
| 95 | +SCIMMY.Resources.ServiceProviderConfig.basepath(ROUTE) |
| 96 | +for (let Resource of Object.values(SCIMMY.Resources.declared())) { |
| 97 | + Resource.basepath(ROUTE) |
| 98 | +} |
| 99 | + |
| 100 | +const SCIMHandler = uWSAsyncHandler(async (res: HttpResponse, req: HttpRequest) => { |
| 101 | + const method = req.getMethod(); |
| 102 | + const url = req.getUrl(); // Full URL path |
| 103 | + const query = req.getQuery(); |
| 104 | + const parsedQuery = Object.fromEntries(new URLSearchParams(query).entries()) |
| 105 | + |
| 106 | + console.log(`GEORG [${method}] ${url}`); |
| 107 | + |
| 108 | + if (url === "/scim/ServiceProviderConfig" && method === "get") { |
| 109 | + res.writeHeader("Content-Type", "application/json").end(JSON.stringify(await new SCIMMY.Resources.ServiceProviderConfig(req.getQuery()).read())); |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + if (url.startsWith("/scim/ResourceTypes") && method === "get") { |
| 114 | + const idMatch = url.match(/^\/scim\/ResourceTypes\/(.+)$/); |
| 115 | + if (!idMatch) { |
| 116 | + try { |
| 117 | + const resourceTypes = await new SCIMMY.Resources.ResourceType(query).read() |
| 118 | + res.writeHeader("Content-Type", "application/scim+json").end(JSON.stringify(resourceTypes)) |
| 119 | + } catch (err) { |
| 120 | + res.writeStatus("400 Bad Request").end(JSON.stringify({ error: String(err) })) |
| 121 | + } |
| 122 | + } |
| 123 | + else { |
| 124 | + const id = querystring.unescape(idMatch[1] ?? ''); |
| 125 | + try { |
| 126 | + const resourceTypes = await new SCIMMY.Resources.ResourceType(id, parsedQuery).read() |
| 127 | + res.writeHeader("Content-Type", "application/scim+json").end(JSON.stringify(resourceTypes)) |
| 128 | + } catch (err) { |
| 129 | + res.writeStatus("400 Bad Request").end(JSON.stringify({ error: String(err) })) |
| 130 | + } |
| 131 | + } |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + if (url.startsWith("/scim/Schemas") && method === "get") { |
| 136 | + const idMatch = url.match(/^\/scim\/Schemas\/(.+)$/); |
| 137 | + if (!idMatch) { |
| 138 | + try { |
| 139 | + const resourceTypes = await new SCIMMY.Resources.Schema(query).read() |
| 140 | + res.writeHeader("Content-Type", "application/scim+json").end(JSON.stringify(resourceTypes)) |
| 141 | + } catch (err) { |
| 142 | + res.writeStatus("400 Bad Request").end(JSON.stringify({ error: String(err) })) |
| 143 | + } |
| 144 | + } |
| 145 | + else { |
| 146 | + const id = querystring.unescape(idMatch[1] ?? ''); |
| 147 | + try { |
| 148 | + const resourceTypes = await new SCIMMY.Resources.Schema(id, parsedQuery).read() |
| 149 | + res.writeHeader("Content-Type", "application/scim+json").end(JSON.stringify(resourceTypes)) |
| 150 | + } catch (err) { |
| 151 | + res.writeStatus("400 Bad Request").end(JSON.stringify({ error: String(err) })) |
| 152 | + } |
| 153 | + } |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + if (url.startsWith("/scim/Users")) { |
| 158 | + const idMatch = url.match(/^\/scim\/Users\/(.+)$/); |
| 159 | + if (!idMatch) { |
| 160 | + if (method === "get") { |
| 161 | + const users = await new SCIMMY.Resources.User(query).read(); |
| 162 | + res.writeHeader("Content-Type", "application/scim+json").end(JSON.stringify(users)); |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + if (method === "post") { |
| 167 | + const body = await parseBody({res}) |
| 168 | + if (body === null) { |
| 169 | + res.writeStatus("400 Bad Request") |
| 170 | + return |
| 171 | + } |
| 172 | + try { |
| 173 | + const user = await new SCIMMY.Resources.User(query).write(body) |
| 174 | + res.writeStatus("201 Created").writeHeader("Content-Type", "application/scim+json").end(JSON.stringify(user)); |
| 175 | + } catch (err) { |
| 176 | + res.writeStatus("400 Bad Request").end(JSON.stringify({ error: String(err) })); |
| 177 | + } |
| 178 | + return |
| 179 | + } |
| 180 | + } else { |
| 181 | + const id = querystring.unescape(idMatch[1] ?? ''); |
| 182 | + console.log('GEORG User id', id) |
| 183 | + |
| 184 | + if (method === "get") { |
| 185 | + try { |
| 186 | + const user = await new SCIMMY.Resources.User(query).read(id) |
| 187 | + res.writeHeader("Content-Type", "application/scim+json") |
| 188 | + res.end(JSON.stringify(user)) |
| 189 | + } catch (err) { |
| 190 | + res.writeStatus("404 Not Found").end(JSON.stringify({ error: "User not found" })) |
| 191 | + } |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + if (method === "patch") { |
| 196 | + const body = await parseBody({res}) |
| 197 | + if (body === null) { |
| 198 | + res.writeStatus("400 Bad Request") |
| 199 | + return |
| 200 | + } |
| 201 | + try { |
| 202 | + const updatedUser = await new SCIMMY.Resources.User(id, parsedQuery).patch(body as any); |
| 203 | + res.writeHeader("Content-Type", "application/scim+json").end(JSON.stringify(updatedUser)); |
| 204 | + } catch (err) { |
| 205 | + res.writeStatus("400 Bad Request").end(JSON.stringify({ error: String(err) })); |
| 206 | + } |
| 207 | + return |
| 208 | + } |
| 209 | + |
| 210 | + if (method === "delete") { |
| 211 | + try { |
| 212 | + await new SCIMMY.Resources.User(id).dispose(); |
| 213 | + res.writeStatus("204 No Content").end(); |
| 214 | + } catch (err) { |
| 215 | + res.writeStatus("404 Not Found").end(JSON.stringify({ error: "User not found" })); |
| 216 | + } |
| 217 | + return |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + res.writeStatus("404 Not Found").end(JSON.stringify({ error: "Endpoint not found" })); |
| 223 | + return |
| 224 | +}) |
| 225 | + |
| 226 | +export default SCIMHandler |
0 commit comments