2021-03-24 17:31:37 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-03-17 09:46:05 +00:00
|
|
|
package signinwithssb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ed25519"
|
|
|
|
|
|
|
|
refs "go.mindeco.de/ssb-refs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// sign-in with ssb uses 256-bit nonces
|
|
|
|
const challengeLength = 32
|
|
|
|
|
2021-03-26 08:47:52 +00:00
|
|
|
// DecodeChallengeString accepts base64 encoded strings and decodes them,
|
|
|
|
// checks their length to be equal to challengeLength,
|
|
|
|
// and returns the decoded bytes
|
2021-03-17 09:46:05 +00:00
|
|
|
func DecodeChallengeString(c string) ([]byte, error) {
|
|
|
|
challengeBytes, err := base64.URLEncoding.DecodeString(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid challenge encoding: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n := len(challengeBytes); n != challengeLength {
|
|
|
|
return nil, fmt.Errorf("invalid challenge length: expected %d but got %d", challengeLength, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return challengeBytes, nil
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:47:52 +00:00
|
|
|
// GenerateChallenge returs a base64 encoded string
|
|
|
|
// with challangeLength bytes of random data
|
2021-03-17 09:46:05 +00:00
|
|
|
func GenerateChallenge() string {
|
|
|
|
buf := make([]byte, challengeLength)
|
|
|
|
rand.Read(buf)
|
|
|
|
return base64.URLEncoding.EncodeToString(buf)
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:58:03 +00:00
|
|
|
// ClientPayload is used to create and verify solutions
|
|
|
|
type ClientPayload struct {
|
2021-03-17 09:46:05 +00:00
|
|
|
ClientID, ServerID refs.FeedRef
|
|
|
|
|
|
|
|
ClientChallenge string
|
|
|
|
ServerChallenge string
|
|
|
|
}
|
|
|
|
|
|
|
|
// recreate the signed message
|
2021-03-26 16:58:03 +00:00
|
|
|
func (cr ClientPayload) createMessage() []byte {
|
2021-03-17 09:46:05 +00:00
|
|
|
var msg bytes.Buffer
|
|
|
|
msg.WriteString("=http-auth-sign-in:")
|
|
|
|
msg.WriteString(cr.ServerID.Ref())
|
|
|
|
msg.WriteString(":")
|
|
|
|
msg.WriteString(cr.ClientID.Ref())
|
|
|
|
msg.WriteString(":")
|
|
|
|
msg.WriteString(cr.ServerChallenge)
|
|
|
|
msg.WriteString(":")
|
|
|
|
msg.WriteString(cr.ClientChallenge)
|
|
|
|
return msg.Bytes()
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:47:52 +00:00
|
|
|
// Sign returns the signature created with the passed privateKey
|
2021-03-26 16:58:03 +00:00
|
|
|
func (cr ClientPayload) Sign(privateKey ed25519.PrivateKey) []byte {
|
2021-03-17 09:46:05 +00:00
|
|
|
msg := cr.createMessage()
|
|
|
|
return ed25519.Sign(privateKey, msg)
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:47:52 +00:00
|
|
|
// Validate checks the signature by calling createMessage() and ed25519.Verify()
|
|
|
|
// together with the ClientID public key.
|
2021-03-26 16:58:03 +00:00
|
|
|
func (cr ClientPayload) Validate(signature []byte) bool {
|
2021-03-17 09:46:05 +00:00
|
|
|
msg := cr.createMessage()
|
|
|
|
return ed25519.Verify(cr.ClientID.PubKey(), msg, signature)
|
|
|
|
}
|