]> gerrit.simantics Code Review - simantics/district.git/blob - authify.js
0a259757b306b4fd25364ee1e7874e2f6cd2042e
[simantics/district.git] / authify.js
1 module.exports = authify
2
3 function authify (authed, parsed, headers, credentials) {
4   if (credentials && credentials.token) {
5     this.log.verbose('request', 'using bearer token for auth')
6     headers.authorization = 'Bearer ' + credentials.token
7
8     return null
9   }
10
11   if (authed) {
12     if (credentials && credentials.username && credentials.password) {
13       var username = encodeURIComponent(credentials.username)
14       var password = encodeURIComponent(credentials.password)
15       parsed.auth = username + ':' + password
16     } else {
17       return new Error(
18         'This request requires auth credentials. Run `npm login` and repeat the request.'
19       )
20     }
21   }
22 }