Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use readKeyFile to read both seed file and public key file #54

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 5 additions & 13 deletions nk/main.go
@@ -1,4 +1,4 @@
// Copyright 2018-2022 The NATS Authors
// Copyright 2018-2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -157,7 +157,7 @@ func sign(fname, keyFile string) {
if err != nil {
log.Fatal(err)
}
log.Printf("%s", base64.StdEncoding.EncodeToString(sigraw))
log.Printf("%s", base64.RawURLEncoding.EncodeToString(sigraw))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in JWT three parts, header.payload.signature, the signature part is encoded in base64 url no padding format according JWT spec. If nk uses base64 url no padding format as signature content, nk command can be used to verify jwt token like this:
nk -verify header.payload -pubin pubin -sigfile signature
Or nk would compain illegle charset error.

}

func verify(fname, keyFile, pubFile, sigFile string) {
Expand All @@ -170,19 +170,11 @@ func verify(fname, keyFile, pubFile, sigFile string) {
var err error
var kp nkeys.KeyPair
if keyFile != "" {
var seed []byte
seed, err = os.ReadFile(keyFile)
if err != nil {
log.Fatal(err)
}
seed := readKeyFile(keyFile)
kp, err = nkeys.FromSeed(seed)
} else {
// Public Key
var public []byte
public, err = os.ReadFile(pubFile)
if err != nil {
log.Fatal(err)
}
public := readKeyFile(pubFile)
kp, err = nkeys.FromPublicKey(string(public))
}
if err != nil {
Expand All @@ -198,7 +190,7 @@ func verify(fname, keyFile, pubFile, sigFile string) {
if err != nil {
log.Fatal(err)
}
sig, err := base64.StdEncoding.DecodeString(string(sigEnc))
sig, err := base64.RawURLEncoding.DecodeString(string(sigEnc))
if err != nil {
log.Fatal(err)
}
Expand Down