-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
406 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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 | ||
|
||
func DecodeChallengeString(c string) ([]byte, error) { | ||
challangeBytes, err := base64.URLEncoding.DecodeString(c) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid challenge encoding: %w", err) | ||
} | ||
|
||
if n := len(challangeBytes); n != challengeLength { | ||
return nil, fmt.Errorf("invalid challenge length: expected %d but got %d", challengeLength, n) | ||
} | ||
|
||
return challangeBytes, nil | ||
} | ||
|
||
func GenerateChallenge() string { | ||
buf := make([]byte, challengeLength) | ||
rand.Read(buf) | ||
return base64.URLEncoding.EncodeToString(buf) | ||
} | ||
|
||
// this structure is used to verify an incoming client response | ||
type ClientRequest struct { | ||
ClientID, ServerID refs.FeedRef | ||
|
||
ClientChallange string | ||
ServerChallange string | ||
} | ||
|
||
// recreate the signed message | ||
func (cr ClientRequest) createMessage() []byte { | ||
var msg bytes.Buffer | ||
msg.WriteString("=http-auth-sign-in:") | ||
msg.WriteString(cr.ClientID.Ref()) | ||
msg.WriteString(":") | ||
msg.WriteString(cr.ServerID.Ref()) | ||
msg.WriteString(":") | ||
msg.WriteString(cr.ClientChallange) | ||
msg.WriteString(":") | ||
msg.WriteString(cr.ServerChallange) | ||
return msg.Bytes() | ||
} | ||
|
||
func (cr ClientRequest) Sign(privateKey ed25519.PrivateKey) []byte { | ||
msg := cr.createMessage() | ||
return ed25519.Sign(privateKey, msg) | ||
} | ||
|
||
func (cr ClientRequest) Validate(signature []byte) bool { | ||
msg := cr.createMessage() | ||
return ed25519.Verify(cr.ClientID.PubKey(), msg, signature) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.