Created
December 4, 2021 19:26
-
-
Save salrashid123/42a55589c4aa9007446dd3aaefd75f94 to your computer and use it in GitHub Desktop.
golang-jwt-yubikey
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"time" | |
"github.com/golang-jwt/jwt" | |
yk "github.com/salrashid123/golang-jwt-yubikey" | |
) | |
var () | |
func main() { | |
ctx := context.Background() | |
var keyctx interface{} | |
claims := &jwt.StandardClaims{ | |
ExpiresAt: time.Now().Add(time.Minute * 1).Unix(), | |
Issuer: "test", | |
} | |
yk.SigningMethodYKRS256.Override() | |
token := jwt.NewWithClaims(yk.SigningMethodYKRS256, claims) | |
config := &yk.YKConfig{ | |
Pin: "123456", | |
} | |
keyctx, err := yk.NewYKContext(ctx, config) | |
if err != nil { | |
log.Fatalf("Unable to initialize tpmJWT: %v", err) | |
} | |
token.Header["kid"] = config.GetKeyID() | |
tokenString, err := token.SignedString(keyctx) | |
if err != nil { | |
log.Fatalf("Error signing %v", err) | |
} | |
fmt.Printf("TOKEN: %s\n", tokenString) | |
// verify with TPM based publicKey | |
keyFunc, err := yk.YKVerfiyKeyfunc(ctx, config) | |
if err != nil { | |
log.Fatalf("could not get keyFunc: %v", err) | |
} | |
vtoken, err := jwt.Parse(tokenString, keyFunc) | |
if err != nil { | |
log.Fatalf("Error verifying token %v", err) | |
} | |
if vtoken.Valid { | |
log.Println(" verified with TPM PublicKey") | |
} | |
// verify with provided RSAPublic key | |
pubKey := config.GetPublicKey() | |
v, err := jwt.Parse(vtoken.Raw, func(token *jwt.Token) (interface{}, error) { | |
return pubKey, nil | |
}) | |
if v.Valid { | |
log.Println(" verified with exported PubicKey") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment