-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
58 lines (50 loc) · 2.29 KB
/
index.ts
File metadata and controls
58 lines (50 loc) · 2.29 KB
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
import * as request from 'request'
class AuthorizationError extends Error {
name: string = "AuthorizationError"
code: string
constructor(code: string, message?: string) {
super(code)
this.code = code
this.message = message === undefined ? "" : message
this.stack = (<any>new Error()).stack
Object.setPrototypeOf(this, AuthorizationError.prototype)
}
}
export function authorize(organization: string) : Function {
return (socket: { request: { _query: { token: undefined; } | undefined; }; }, next: { (arg0: AuthorizationError): void; (arg0: AuthorizationError): void; (): void; (arg0: AuthorizationError): void; (arg0: AuthorizationError): void; (arg0: AuthorizationError): void; }) => {
if(socket.request._query === undefined ||
socket.request._query.token === undefined){
next(new AuthorizationError('missing_token'))
} else {
request({
url: 'https://api.github.com/user/orgs',
headers: {
'Authorization': 'token ' + socket.request._query.token,
'User-Agent': 'ua',
}
}, (err: any, res: any, body: string) => {
if(err){
next(new AuthorizationError('failed_authorization', err))
} else if(body){
let parsedBody = JSON.parse(body)
if(parsedBody){
if(parsedBody.length > 0){
const isMemberOfOrganization = parsedBody.some((organizationItem: { login: any; }) => {
return organizationItem.login === organization
})
if(isMemberOfOrganization){
next()
} else {
next(new AuthorizationError('not_a_member'))
}
} else {
next(new AuthorizationError('failed_authorization', parsedBody.message))
}
} else {
next(new AuthorizationError('failed_authorization'))
}
}
})
}
}
}