This repository was archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblih.js
77 lines (63 loc) · 2.61 KB
/
blih.js
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
const crypto = require('crypto')
const request = require('request')
class Blih {
constructor(email, token) {
this.baseUrl = 'https://blih.epitech.eu/'
this.userData = {
login: email,
token
}
}
static generateToken(password) {
return crypto.createHash('sha512').update(password, 'utf8').digest('hex')
}
getRepositories(callback) {
this.createRequest({ verb: 'GET', path: 'repositories' }, undefined, callback)
}
getAcl(repository, callback) {
this.createRequest({ verb: 'GET', path: `repositories/${repository}/acl` }, undefined, callback)
}
getRepositoriesInfo(repository, callback) {
this.createRequest({ verb: 'GET', path: `repositories/${repository}` }, undefined, callback)
}
getSshKey(callback) {
this.createRequest({ verb: 'GET', path: 'sshkey' }, undefined, callback)
}
createRepository(repository, callback) {
this.createRequest({ verb: 'POST', path: 'repositories' }, { name: repository, type: 'git' }, callback)
}
setAcl(repository, username, rights, callback) {
this.createRequest({ verb: 'POST', path: `repositories/${repository}/acl` }, { acl: rights, user: username }, callback)
}
setSshKey(sshkey, callback) {
this.createRequest({ verb: 'POST', path: 'sshkey' }, { sshkey }, callback)
}
deleteSshKey(keyid, callback) {
this.createRequest({ verb: 'DELETE', path: `sshkey/${keyid}` }, undefined, callback)
}
deleteRepository(repository, callback) {
this.createRequest({ verb: 'DELETE', path: `repositories/${repository}` }, undefined, callback)
}
sendRequest(signedJson, verb, path, callback) {
if (verb !== 'GET' && verb !== 'POST' && verb !== 'DELETE') return callback('Error: Invalid http method')
if (path.substring(0, 12) !== 'repositories' && path.substring(0, 6) !== 'sshkey') return callback('Error: Invalid path data')
const options = { uri: `${this.baseUrl}user/${path}`, method: verb, json: signedJson }
return request(options, (error, response, body) => {
if (body.error !== undefined) error = body.error
callback(error, body)
})
}
createRequest(argData, postData, callback) {
const signatureData = { user: this.userData.login, signature: '' }
const hashing = crypto.createHmac('sha512', this.userData.token)
hashing.update(this.userData.login)
if (postData !== undefined) {
const string = JSON.stringify(postData, null, 4)
hashing.update(string)
signatureData.data = postData
}
signatureData.signature = hashing.digest('hex').toString()
return this.sendRequest(signatureData, argData.verb, argData.path, callback)
}
}
module.exports = Blih