|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/x509" |
| 7 | + "encoding/json" |
| 8 | + "encoding/pem" |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/golang-jwt/jwt/v5" |
| 14 | +) |
| 15 | + |
| 16 | +const privateKeyFileName = "private-key.pem" |
| 17 | +const publicKeyFileName = "public-key.pem" |
| 18 | + |
| 19 | +func main() { |
| 20 | + fmt.Println() |
| 21 | + privateKey, err := getPrivateKey() |
| 22 | + if err != nil { |
| 23 | + fmt.Fprintf(os.Stderr, "Error getting private key: %v\n", err) |
| 24 | + os.Exit(1) |
| 25 | + } |
| 26 | + |
| 27 | + jsonPubKey, err := getPublicKeyJson(privateKey) |
| 28 | + if err != nil { |
| 29 | + fmt.Fprintf(os.Stderr, "Error getting public key JSON: %v\n", err) |
| 30 | + os.Exit(1) |
| 31 | + } |
| 32 | + fmt.Println(jsonPubKey) |
| 33 | + fmt.Println() |
| 34 | + |
| 35 | + signedJWT, err := generateSignedJWT(privateKey) |
| 36 | + if err != nil { |
| 37 | + fmt.Fprintf(os.Stderr, "Error generating signed JWT: %v\n", err) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + fmt.Println(signedJWT) |
| 41 | + fmt.Println() |
| 42 | +} |
| 43 | + |
| 44 | +func generateSignedJWT(priv *rsa.PrivateKey) (string, error) { |
| 45 | + now := time.Now() |
| 46 | + issued := now.Unix() |
| 47 | + expires := now.AddDate(1, 0, 0).Unix() |
| 48 | + claims := jwt.MapClaims{ |
| 49 | + "iat": issued, |
| 50 | + "exp": expires, |
| 51 | + } |
| 52 | + |
| 53 | + token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims) |
| 54 | + return token.SignedString(priv) |
| 55 | +} |
| 56 | + |
| 57 | +func getPrivateKey() (*rsa.PrivateKey, error) { |
| 58 | + if _, err := os.Stat(privateKeyFileName); err == nil { |
| 59 | + return loadPrivateKey() |
| 60 | + } else if os.IsNotExist(err) { |
| 61 | + return createPrivateKey() |
| 62 | + } else { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func loadPrivateKey() (*rsa.PrivateKey, error) { |
| 68 | + keyData, err := os.ReadFile(privateKeyFileName) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + block, _ := pem.Decode(keyData) |
| 74 | + if block == nil || block.Type != "RSA PRIVATE KEY" { |
| 75 | + return nil, fmt.Errorf("failed to decode PEM block containing private key") |
| 76 | + } |
| 77 | + |
| 78 | + priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + return priv, nil |
| 83 | +} |
| 84 | + |
| 85 | +func createPrivateKey() (*rsa.PrivateKey, error) { |
| 86 | + privateKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + if err := writePrivateKey(privateKey); err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + if err := writePublicKey(privateKey); err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + return privateKey, nil |
| 100 | +} |
| 101 | + |
| 102 | +func writePrivateKey(priv *rsa.PrivateKey) error { |
| 103 | + keyFile, err := os.Create(privateKeyFileName) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + defer keyFile.Close() |
| 108 | + |
| 109 | + pemBlock := &pem.Block{ |
| 110 | + Type: "RSA PRIVATE KEY", |
| 111 | + Bytes: x509.MarshalPKCS1PrivateKey(priv), |
| 112 | + } |
| 113 | + if err := pem.Encode(keyFile, pemBlock); err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func writePublicKey(priv *rsa.PrivateKey) error { |
| 121 | + publicKey := &priv.PublicKey |
| 122 | + |
| 123 | + keyFile, err := os.Create(publicKeyFileName) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + defer keyFile.Close() |
| 128 | + |
| 129 | + pemBlock := &pem.Block{ |
| 130 | + Type: "RSA PUBLIC KEY", |
| 131 | + Bytes: x509.MarshalPKCS1PublicKey(publicKey), |
| 132 | + } |
| 133 | + if err := pem.Encode(keyFile, pemBlock); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +func getPublicKeyJson(priv *rsa.PrivateKey) (string, error) { |
| 141 | + publicKey := &priv.PublicKey |
| 142 | + keyData := x509.MarshalPKCS1PublicKey(publicKey) |
| 143 | + block := pem.EncodeToMemory(&pem.Block{ |
| 144 | + Type: "RSA PUBLIC KEY", |
| 145 | + Bytes: keyData, |
| 146 | + }) |
| 147 | + |
| 148 | + keys := map[string]string{ |
| 149 | + "key1": string(block), |
| 150 | + } |
| 151 | + |
| 152 | + jsonData, err := json.Marshal(keys) |
| 153 | + if err != nil { |
| 154 | + return "", err |
| 155 | + } |
| 156 | + return string(jsonData), nil |
| 157 | +} |
0 commit comments