-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathauth.go
51 lines (42 loc) · 996 Bytes
/
auth.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 arangolite
import (
"context"
"net/http"
"github.com/solher/arangolite/v2/requests"
)
type authentication interface {
Setup(ctx context.Context, db *Database) error
Apply(req *http.Request) error
}
type basicAuth struct {
username, password string
}
func (a *basicAuth) Setup(ctx context.Context, db *Database) error {
return nil
}
func (a *basicAuth) Apply(req *http.Request) error {
req.SetBasicAuth(a.username, a.password)
return nil
}
type jwtAuth struct {
username, password string
jwt string
}
func (a *jwtAuth) Setup(ctx context.Context, db *Database) error {
res, err := db.Send(ctx, &requests.JWTAuth{Username: a.username, Password: a.password})
if err != nil {
return err
}
jwtRes := struct {
JWT string `json:"jwt"`
}{}
if err := res.Unmarshal(&jwtRes); err != nil {
return err
}
a.jwt = jwtRes.JWT
return nil
}
func (a *jwtAuth) Apply(req *http.Request) error {
req.Header.Set("Authorization", "bearer "+a.jwt)
return nil
}