forked from jeroenvanmaanen/dendrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
142 lines (123 loc) · 4.22 KB
/
authentication.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
132
133
134
135
136
137
138
139
140
141
142
package authentication
import (
bytes "bytes"
log "log"
strings "strings"
rand "crypto/rand"
sha256 "crypto/sha256"
base64 "encoding/base64"
jwt "github.com/pascaldekloe/jwt"
ssh "golang.org/x/crypto/ssh"
axon_utils "github.com/dendrite2go/dendrite/src/pkg/axon_utils"
grpc_config "github.com/dendrite2go/dendrite/src/pkg/grpc/dendrite_config"
trusted "github.com/dendrite2go/dendrite/src/pkg/trusted"
)
var acceptedCredentials map[string]string
func Init() {
acceptedCredentials = make(map[string]string)
}
func UnsafeSetCredentials(credentials *grpc_config.Credentials) {
acceptedCredentials[credentials.Identifier] = credentials.Secret
}
func SetCredentials(credentials *grpc_config.Credentials, clientConnection *axon_utils.ClientConnection) error {
payload := credentials.Identifier + "=" + credentials.Secret
signatureKey, e := trusted.GetKeyManagerKey(credentials.Signature.SignatureName)
if e != nil {
return e
}
signature := ssh.Signature{
Format: credentials.Signature.Format,
Blob: credentials.Signature.Blob,
Rest: credentials.Signature.Rest,
}
e = signatureKey.Verify([]byte(payload), &signature)
if e != nil {
return e
}
log.Printf("Set credentials: %v: %v", credentials.Identifier, credentials.Secret)
var encryptedSecret string
if credentials.Secret == "" {
encryptedSecret = ""
} else {
encryptedSecret, e = trusted.EncryptString(credentials.Secret)
if e != nil {
return e
}
}
commandCredentials := grpc_config.Credentials{
Identifier: credentials.Identifier,
Secret: encryptedSecret,
}
command := grpc_config.RegisterCredentialsCommand{
Credentials: &commandCredentials,
}
e = axon_utils.SendCommand("RegisterCredentialsCommand", &command, clientConnection)
return e
}
func Authenticate(username string, password string) bool {
encryptedHashedPassword := acceptedCredentials[username]
log.Printf("Authenticate: %v: encryptedHashedPassword: '%v'", username, encryptedHashedPassword)
hashedPassword, e := trusted.DecryptString(encryptedHashedPassword)
if e != nil {
log.Printf("Authenticate: %v: could not decrypt hashed password: %v", username, e)
return false
}
log.Printf("Authenticate: %v: hashedPassword: '%v'", username, hashedPassword)
parts := strings.Split(hashedPassword, ":")
if len(parts) != 3 {
log.Printf("Authenticate: %v: number of parts: %v", username, len(parts))
return false
}
if parts[0] != "sha256" {
log.Printf("Authenticate: %v: hash type: %v", username, parts[0])
return false
}
salt, e := base64.RawStdEncoding.DecodeString(parts[1])
if e != nil {
log.Printf("Authenticate: %v: could not base64 decode salt: %v", username, parts[1])
return false
}
storedHash, e := base64.RawStdEncoding.DecodeString(parts[2])
if e != nil {
log.Printf("Authenticate: %v: could not base64 decode store hash: %v", username, parts[2])
return false
}
blob := append(salt, []byte(password)...)
givenHash := sha256.Sum256(blob)
log.Printf("Authenticate: %v: given hash: %v", username, givenHash)
return bytes.Compare(givenHash[:], storedHash) == 0
}
func CheckKnown(credentials *grpc_config.Credentials, projection *Projection) bool {
newHashedPassword, e := trusted.DecryptString(credentials.Secret)
if e != nil {
log.Printf("Authenticate: %v: cannot decrypt new hash: assuming unknown", credentials.Identifier)
return false
}
oldHashedPassword, e := trusted.DecryptString((*projection).Credentials[credentials.Identifier])
if e != nil {
log.Printf("Authenticate: %v: cannot decrypt old hash: assuming unknown", credentials.Identifier)
return false
}
return newHashedPassword == oldHashedPassword
}
func Encode(password string) string {
salt := make([]byte, 32)
_, _ = rand.Reader.Read(salt)
passwordBytes := []byte(password)
blob := append(salt, passwordBytes...)
hash := sha256.Sum256(blob)
var builder strings.Builder
builder.WriteString("sha256:")
builder.WriteString(base64.RawStdEncoding.EncodeToString(salt))
builder.WriteString(":")
builder.WriteString(base64.RawStdEncoding.EncodeToString(hash[:]))
return builder.String()
}
func Verify(accessToken *grpc_config.AccessToken) bool {
publicKey, e := trusted.GetRsaPublicKey()
if e != nil {
return false
}
_, e = jwt.RSACheck([]byte(accessToken.Jwt), publicKey)
return e == nil
}