forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for handleComAtprotoAccountCreate and handleComAtprotoSessi…
…onCreate
- Loading branch information
Showing
10 changed files
with
259 additions
and
201 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cliutil | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/lestrrat-go/jwx/v2/jwa" | ||
"github.com/lestrrat-go/jwx/v2/jwk" | ||
"github.com/whyrusleeping/go-did" | ||
) | ||
|
||
// LoadKeyFromFile reads the private key from file | ||
func LoadKeyFromFile(kfile string) (*did.PrivKey, error) { | ||
kb, err := os.ReadFile(kfile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sk, err := jwk.ParseKey(kb) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var spk ecdsa.PrivateKey | ||
if err := sk.Raw(&spk); err != nil { | ||
return nil, err | ||
} | ||
curve, ok := sk.Get("crv") | ||
if !ok { | ||
return nil, fmt.Errorf("need a curve set") | ||
} | ||
|
||
var out string | ||
kts := string(curve.(jwa.EllipticCurveAlgorithm)) | ||
switch kts { | ||
case "P-256": | ||
out = did.KeyTypeP256 | ||
default: | ||
return nil, fmt.Errorf("unrecognized key type: %s", kts) | ||
} | ||
|
||
return &did.PrivKey{ | ||
Raw: &spk, | ||
Type: out, | ||
}, nil | ||
} | ||
|
||
// GenerateKeyToFile makes the private key and store it into the file | ||
func GenerateKeyToFile(fname string) error { | ||
raw, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
return fmt.Errorf("failed to generate new ECDSA private key: %s", err) | ||
} | ||
|
||
key, err := jwk.FromRaw(raw) | ||
if err != nil { | ||
return fmt.Errorf("failed to create ECDSA key: %s", err) | ||
} | ||
|
||
if _, ok := key.(jwk.ECDSAPrivateKey); !ok { | ||
return fmt.Errorf("expected jwk.ECDSAPrivateKey, got %T", key) | ||
} | ||
|
||
key.Set(jwk.KeyIDKey, "mykey") | ||
|
||
buf, err := json.MarshalIndent(key, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal key into JSON: %w", err) | ||
} | ||
|
||
// ensure data directory exists; won't error if it does | ||
os.MkdirAll(filepath.Dir(fname), os.ModePerm) | ||
|
||
return os.WriteFile(fname, buf, 0664) | ||
} |
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,30 @@ | ||
package cliutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/whyrusleeping/go-did" | ||
) | ||
|
||
func TestKeyGenerationAndLoading(t *testing.T) { | ||
tempdir, err := os.MkdirTemp("", "msttest-") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(tempdir) | ||
fkey := filepath.Join(tempdir, "test.key") | ||
err = GenerateKeyToFile(fkey) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
key, err := LoadKeyFromFile(fkey) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if key.Type != did.KeyTypeP256 { | ||
t.Fatalf("unexpected type of the key %s", key.KeyType()) | ||
} | ||
} |
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
Oops, something went wrong.