-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_request.go
81 lines (64 loc) · 1.63 KB
/
auth_request.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
package octokey
import (
"crypto"
"crypto/rsa"
"crypto/sha1"
"github.com/octokey/octokey-go/buffer"
)
type Signer interface {
SignPKCS1v15(hash crypto.Hash, hashed []byte) (s []byte, err error)
PublicKey() *rsa.PublicKey
Username() string
}
type AuthRequest struct {
ChallengeBuffer *buffer.Buffer
RequestUrl string
Username string
ServiceName string
AuthMethod string
SigningAlgorithm string
PublicKey *rsa.PublicKey
SignatureBuffer *buffer.Buffer
}
const SERVICE_NAME = "octokey-auth"
const AUTH_METHOD = "publickey"
const SIGNING_ALGORITHM = "ssh-rsa"
func (O *Octokey) SignChallenge(challenge string, requestUrl string, signer Signer) (string, error) {
a := AuthRequest{
ChallengeBuffer: buffer.NewBuffer(challenge),
RequestUrl: requestUrl,
Username: signer.Username(),
ServiceName: SERVICE_NAME,
AuthMethod: AUTH_METHOD,
SigningAlgorithm: SIGNING_ALGORITHM,
}
return a.Sign(signer)
}
func (a *AuthRequest) Sign(s Signer) (string, error) {
a.PublicKey = s.PublicKey()
b := a.unsignedBuffer()
h := sha1.New()
h.Write(b.Raw())
digest := h.Sum(nil)
sig, err := s.SignPKCS1v15(crypto.SHA1, digest)
if err != nil {
return "", err
}
b.AddVarBytes(sig)
if b.Error != nil {
return "", b.Error
}
return b.String(), nil
}
func (a *AuthRequest) unsignedBuffer() *buffer.Buffer {
b := buffer.Buffer{}
b.AddBuffer(a.ChallengeBuffer)
b.AddString(a.RequestUrl)
b.AddString(a.Username)
b.AddString(a.ServiceName)
b.AddString(a.AuthMethod)
b.AddString(a.SigningAlgorithm)
// b.AddVarBytes(publicKeyBytes(a.PublicKey))
panic("todo")
return &b
}