NewToken creates new JWT token for the gien username. It embedds the given public key as kontrolKey and signs the token with the private one.
(username, private, public string)
| 34 | // NewToken creates new JWT token for the gien username. It embedds the given |
| 35 | // public key as kontrolKey and signs the token with the private one. |
| 36 | func NewToken(username, private, public string) *jwt.Token { |
| 37 | tknID := uuid.Must(uuid.NewV4()) |
| 38 | |
| 39 | hostname, err := os.Hostname() |
| 40 | if err != nil { |
| 41 | panic(err) |
| 42 | } |
| 43 | |
| 44 | if username == "" { |
| 45 | username = "testuser" |
| 46 | } |
| 47 | |
| 48 | if testuser := os.Getenv("TESTKEY_USERNAME"); testuser != "" { |
| 49 | username = testuser |
| 50 | } |
| 51 | |
| 52 | claims := &kitekey.KiteClaims{ |
| 53 | StandardClaims: jwt.StandardClaims{ |
| 54 | Issuer: "testuser", |
| 55 | Subject: username, |
| 56 | Audience: hostname, |
| 57 | IssuedAt: time.Now().UTC().Unix(), |
| 58 | Id: tknID.String(), |
| 59 | }, |
| 60 | KontrolKey: public, |
| 61 | KontrolURL: "http://localhost:4000/kite", |
| 62 | } |
| 63 | |
| 64 | token := jwt.NewWithClaims(jwt.GetSigningMethod("RS256"), claims) |
| 65 | |
| 66 | rsaPrivate, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(private)) |
| 67 | if err != nil { |
| 68 | panic(err) |
| 69 | } |
| 70 | |
| 71 | token.Raw, err = token.SignedString(rsaPrivate) |
| 72 | if err != nil { |
| 73 | panic(err) |
| 74 | } |
| 75 | |
| 76 | // verify the token |
| 77 | _, err = jwt.ParseWithClaims(token.Raw, claims, func(*jwt.Token) (interface{}, error) { |
| 78 | return jwt.ParseRSAPublicKeyFromPEM([]byte(public)) |
| 79 | }) |
| 80 | |
| 81 | if err != nil { |
| 82 | panic(err) |
| 83 | } |
| 84 | |
| 85 | token.Valid = true |
| 86 | return token |
| 87 | |
| 88 | } |
| 89 | |
| 90 | func NewConfig() *config.Config { |
| 91 | conf := config.New() |
no test coverage detected