-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (41 loc) · 1.14 KB
/
main.go
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
package main
import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/g-harel/targetblank/internal/crypto"
"github.com/g-harel/targetblank/internal/handler"
"github.com/g-harel/targetblank/services/secrets"
"github.com/g-harel/targetblank/services/storage"
)
var secretsKey = secrets.Key
var storagePageRead = storage.PageRead
// Authenticate responds with a token when given valid credentials.
func Authenticate(req *handler.Request, res *handler.Response) *handler.Error {
addr, funcErr := req.Param("addr")
if funcErr != nil {
return funcErr
}
page, err := storagePageRead(addr)
if err != nil {
return handler.InternalErr("read page: %v", err)
}
if page == nil {
return handler.ObfuscatedAuthErr()
}
if !crypto.HashCheck(req.Body, page.Password) {
return handler.ObfuscatedAuthErr()
}
key, err := secretsKey()
if err != nil {
return handler.InternalErr("read secret key: %v", err)
}
token, err := handler.CreateToken(key, addr)
if err != nil {
return handler.InternalErr("create token: %v", err)
}
res.Body = token
res.ContentType("text/plain")
return nil
}
func main() {
lambda.Start(handler.New(Authenticate))
}