-
Notifications
You must be signed in to change notification settings - Fork 13
/
oauth_ed25519.go
131 lines (105 loc) · 3.11 KB
/
oauth_ed25519.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package mixin
import (
"context"
"crypto/ed25519"
"errors"
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/go-resty/resty/v2"
)
type OauthKeystore struct {
ClientID string `json:"client_id,omitempty"`
AuthID string `json:"authorization_id,omitempty"`
Scope string `json:"scope,omitempty"`
PrivateKey string `json:"private_key,omitempty"`
VerifyKey string `json:"ed25519,omitempty"`
}
func AuthorizeEd25519(ctx context.Context, clientID, clientSecret string, code string, verifier string, privateKey ed25519.PrivateKey) (*OauthKeystore, error) {
public := privateKey.Public().(ed25519.PublicKey)
params := map[string]interface{}{
"client_id": clientID,
"client_secret": clientSecret,
"code": code,
"code_verifier": verifier,
"ed25519": ed25519Encoding.EncodeToString(public),
}
resp, err := Request(ctx).SetBody(params).Post("/oauth/token")
if err != nil {
return nil, err
}
var key OauthKeystore
if err := UnmarshalResponse(resp, &key); err != nil {
return nil, err
}
key.ClientID = clientID
key.PrivateKey = ed25519Encoding.EncodeToString(privateKey)
return &key, nil
}
type OauthKeystoreAuth struct {
*OauthKeystore
signMethod jwt.SigningMethod
signKey interface{}
verifyKey interface{}
}
func AuthFromOauthKeystore(store *OauthKeystore) (*OauthKeystoreAuth, error) {
auth := &OauthKeystoreAuth{
OauthKeystore: store,
signMethod: Ed25519SigningMethod,
}
sign, err := ed25519Encoding.DecodeString(store.PrivateKey)
if err != nil {
return nil, err
}
auth.signKey = (ed25519.PrivateKey)(sign)
verify, err := ed25519Encoding.DecodeString(store.VerifyKey)
if err != nil {
return nil, err
}
auth.verifyKey = (ed25519.PublicKey)(verify)
return auth, nil
}
func (o *OauthKeystoreAuth) SignToken(signature, requestID string, exp time.Duration) string {
jwtMap := jwt.MapClaims{
"iss": o.ClientID,
"aid": o.AuthID,
"scp": o.Scope,
"iat": time.Now().Unix(),
"exp": time.Now().Add(exp).Unix(),
"sig": signature,
"jti": requestID,
}
token, err := jwt.NewWithClaims(o.signMethod, jwtMap).SignedString(o.signKey)
if err != nil {
panic(err)
}
return token
}
func (o *OauthKeystoreAuth) EncryptPin(pin string) string {
panic(errors.New("[oauth auth] encrypt pin: forbidden"))
}
func (o *OauthKeystoreAuth) Verify(resp *resty.Response) error {
verifyToken := resp.Header().Get(xIntegrityToken)
if verifyToken == "" && IsErrorCodes(UnmarshalResponse(resp, nil), Unauthorized) {
return nil
}
var claim struct {
jwt.StandardClaims
Sign string `json:"sig,omitempty"`
}
if _, err := jwt.ParseWithClaims(verifyToken, &claim, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*EdDSASigningMethod); !ok {
return nil, jwt.ErrInvalidKeyType
}
return o.verifyKey, nil
}); err != nil {
return err
}
if expect, got := claim.Id, resp.Header().Get(xRequestID); expect != got {
return fmt.Errorf("token.jti mismatch, expect %q but got %q", expect, got)
}
if expect, got := claim.Sign, SignResponse(resp); expect != got {
return fmt.Errorf("token.sig mismatch, expect %q but got %q", expect, got)
}
return nil
}