-
Notifications
You must be signed in to change notification settings - Fork 1
/
scim.ts
136 lines (119 loc) · 3.65 KB
/
scim.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { GoogleUser } from "../google/types"
import { GitlabScimUser, GitlabScimUsersListResponse, GitlabUserUpdate, GitlabUserUpdateOperation } from "./types"
import { Gitlab } from './index';
import { getSecretFromAws } from "../utils/aws";
import { logger } from "../utils/logging";
import fs from 'fs';
const GITLAB_SCIM_TOKEN = await resolveGitlabScimToken()
async function resolveGitlabScimToken(): Promise<string> {
if (process.env.GITLAB_SCIM_TOKEN_SECRET !== undefined) {
return await getSecretFromAws(process.env.GITLAB_SCIM_TOKEN_SECRET)
}
if (process.env.GITLAB_SCIM_TOKEN_FILE !== undefined) {
if (!fs.existsSync(process.env.GITLAB_SCIM_TOKEN_FILE)) {
throw Error(`Gitlab SCIM token file does not exist: ${process.env.GITLAB_SCIM_TOKEN_FILE}`)
}
return (fs.readFileSync(process.env.GITLAB_SCIM_TOKEN_FILE)).toString()
}
if (process.env.GITLAB_SCIM_TOKEN !== undefined) {
return process.env.GITLAB_SCIM_TOKEN
}
throw Error("gitlab scim token was not provided.")
}
export class GitlabScim extends Gitlab {
headers: { Authorization: string, "Content-Type": string }
constructor() {
super()
this.url = `${this.url}/scim/v2/groups/${this.group}/Users`
this.headers = {
Authorization: `Bearer ${GITLAB_SCIM_TOKEN}`,
"Content-Type": "application/scim+json"
}
}
async createUser(user: GoogleUser) {
console.log(await this.request("/", {
method: "POST",
body: JSON.stringify({
externalId: user.primaryEmail,
userName: `${user.name.givenName[0]}${user.name.familyName}`,
active: null,
name: {
formatted: user.name.fullName,
familyName: user.name.familyName,
givenName: user.name.givenName
},
schemas: ["urn:ietf:params:scim:schemas:core:2.0:User"],
meta: { resourceType: "User" },
emails: [
{
type: "work",
value: user.primaryEmail,
primary: true
}
]
})
}))
}
async activateUser(user: GitlabScimUser) {
this.request(
`/${user.id}`,
{
method: 'PATCH',
body: JSON.stringify({
Operations: [
{
op: "Update",
path: "active",
value: true
}
]
})
}
)
}
async removeScimUser(user: GitlabScimUser) {
this.request(
`/${user.id}`,
{
method: 'DELETE'
}
)
}
async listScimUsers(): Promise<{ [key: string]: GitlabScimUser }> {
var users: { [key: string]: GitlabScimUser } = {}
var startIndex = 1
while (startIndex > -1) {
const resp = await this.request("", {}) as GitlabScimUsersListResponse
for (var u of resp.Resources) {
for (const email of u.emails) {
if (email.primary) {
users[email.value] = u
break
}
}
}
if (startIndex + resp.itemsPerPage < resp.totalResults) {
startIndex += resp.itemsPerPage
} else {
startIndex = -1
}
}
return users
}
async updateUser(change: GitlabUserUpdate) {
switch (change.op) {
case GitlabUserUpdateOperation.ADD:
await this.createUser(change.user as GoogleUser)
logger.info(`user ${change.user.id} created.`)
break;
case GitlabUserUpdateOperation.REMOVE:
await this.removeScimUser(change.user as GitlabScimUser)
logger.info(`user ${change.user.id} removed.`)
break;
case GitlabUserUpdateOperation.ACTIVATE:
await this.activateUser(change.user as GitlabScimUser)
logger.info(`user ${change.user.id} activate.`)
break;
}
}
}