10 examples of 'golang jwt' in Go

Every line of 'golang jwt' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
265func (internet Internet) Jwt(v reflect.Value) (interface{}, error) {
266 return internet.jwt()
267}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

92func (m *CreateIamTokenRequest) GetJwt() string {
93 if x, ok := m.GetIdentity().(*CreateIamTokenRequest_Jwt); ok {
94 return x.Jwt
95 }
96 return ""
97}
33func DecodeJwt(v string) []string {
34 var splits = strings.Split(v, ".")
35 var jwt []string
36 for _, split := range splits {
37 str, err := base64.RawURLEncoding.DecodeString(split)
38
39 if err != nil {
40 fmt.Println(err)
41 continue
42 }
43
44 jwt = append(jwt, string(str))
45 }
46 return jwt
47}
120func printJWT(parts []string) {
121 fmt.Println("JOSE Header:")
122 fmt.Println(prettifyPart(parts[0]))
123 fmt.Printf("\nJWS Payload:\n")
124 fmt.Println(prettifyPart(parts[1]))
125}
139func (a *Claims) ToJWT(privateKey interface{}) (string, error) {
140 claims, err := a.ToClaims()
141 if err != nil {
142 return "", err
143 }
144 return jwt.CreateToken(claims, privateKey)
145}
270func Jwt() string {
271 return singleFakeData(JWT, func() interface{} {
272 i := Internet{}
273 p, err := i.jwt()
274 if err != nil {
275 panic(err.Error())
276 }
277 return p
278 }).(string)
279}
78func (auth AuthSimpleJwt) MakeJWT(validFor time.Duration, id string) (string, error) {
79 if auth.Issuer == "" {
80 return "", errors.New("SimpleJWT must have an Issuer")
81 }
82 if len(auth.SigningKey) == 0 {
83 return "", errors.New("SimpleJWT must have a SigningKey")
84 }
85 if id == "" {
86 return "", errors.New("SimpleJWT requires an 'id'")
87 }
88 if validFor == 0 {
89 validFor = 15 * time.Minute
90 }
91 claims := jwt.MapClaims{}
92 claims["iss"] = auth.Issuer
93 claims["exp"] = time.Now().Add(validFor).Unix()
94 claims["iat"] = time.Now().Add(-5 * time.Second).Unix() // skew
95 if auth.Audience != "" {
96 claims["aud"] = auth.Audience
97 }
98 if id != "" {
99 claims["sub"] = id
100 }
101 token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), claims)
102 jwtStr, err := token.SignedString(auth.SigningKey)
103 if err != nil {
104 return "", err
105 }
106 return jwtStr, nil
107}
283func createJWT(id string, guilds []string, expiration int64) (string, error) {
284 // Create the Claims
285 claims := Claims{
286 id,
287 guilds,
288 jwt.StandardClaims{
289 ExpiresAt: expiration,
290 Issuer: "MMB",
291 },
292 }
293
294 token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
295 return token.SignedString(mySigningKey)
296}
262func jwtParse(secret []byte, token string) (*jwt.Token, error) {
263 return jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
264 return secret, nil
265 })
266}
91func (h *webHandler) jwtKeyFunc(token *jwt.Token) (interface{}, error) {
92 if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
93 return nil, fmt.Errorf("Unexpected signing method")
94 }
95
96 return h.jwtKey, nil
97}

Related snippets