forked from keys-pub/keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
58 lines (48 loc) · 1.33 KB
/
echo.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
52
53
54
55
56
57
58
package services
import (
"context"
"net/url"
"strings"
"github.com/burlingtonbertie99/mykeys/http"
"github.com/burlingtonbertie99/mykeys/user"
"github.com/pkg/errors"
)
type echo struct{}
// Echo service.
var Echo = &echo{}
func (s *echo) ID() string {
return "echo"
}
func (s *echo) Request(ctx context.Context, client http.Client, usr *user.User) (user.Status, []byte, error) {
ur, err := url.Parse(usr.URL)
if err != nil {
return user.StatusFailure, nil, err
}
if ur.Scheme != "test" {
return user.StatusFailure, nil, errors.Errorf("invalid scheme for echo")
}
if ur.Host != "echo" {
return user.StatusFailure, nil, errors.Errorf("invalid host for echo")
}
path := ur.Path
path = strings.TrimPrefix(path, "/")
paths := strings.Split(path, "/")
if len(paths) != 3 {
return user.StatusFailure, nil, errors.Errorf("path invalid %s", path)
}
msg, err := url.QueryUnescape(paths[2])
if err != nil {
return user.StatusFailure, nil, err
}
if err := usr.Verify(msg); err != nil {
return user.StatusFailure, nil, err
}
return user.StatusOK, []byte(msg), nil
}
func (s *echo) Verify(ctx context.Context, b []byte, usr *user.User) (user.Status, *Verified, error) {
status, statement, err := user.FindVerify(usr, b, false)
if err != nil {
return status, nil, err
}
return status, &Verified{Statement: statement}, nil
}