github.com/nats-io/jwt/v2@v2.5.6/v1compat/user_claims.go (about) 1 /* 2 * Copyright 2018-2019 The NATS Authors 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package jwt 17 18 import ( 19 "errors" 20 21 "github.com/nats-io/nkeys" 22 ) 23 24 // User defines the user specific data in a user JWT 25 type User struct { 26 Permissions 27 Limits 28 BearerToken bool `json:"bearer_token,omitempty"` 29 } 30 31 // Validate checks the permissions and limits in a User jwt 32 func (u *User) Validate(vr *ValidationResults) { 33 u.Permissions.Validate(vr) 34 u.Limits.Validate(vr) 35 // When BearerToken is true server will ignore any nonce-signing verification 36 } 37 38 // UserClaims defines a user JWT 39 type UserClaims struct { 40 ClaimsData 41 User `json:"nats,omitempty"` 42 // IssuerAccount stores the public key for the account the issuer represents. 43 // When set, the claim was issued by a signing key. 44 IssuerAccount string `json:"issuer_account,omitempty"` 45 } 46 47 // NewUserClaims creates a user JWT with the specific subject/public key 48 func NewUserClaims(subject string) *UserClaims { 49 if subject == "" { 50 return nil 51 } 52 c := &UserClaims{} 53 c.Subject = subject 54 return c 55 } 56 57 // Encode tries to turn the user claims into a JWT string 58 func (u *UserClaims) Encode(pair nkeys.KeyPair) (string, error) { 59 if !nkeys.IsValidPublicUserKey(u.Subject) { 60 return "", errors.New("expected subject to be user public key") 61 } 62 u.ClaimsData.Type = UserClaim 63 return u.ClaimsData.Encode(pair, u) 64 } 65 66 // DecodeUserClaims tries to parse a user claims from a JWT string 67 func DecodeUserClaims(token string) (*UserClaims, error) { 68 v := UserClaims{} 69 if err := Decode(token, &v); err != nil { 70 return nil, err 71 } 72 return &v, nil 73 } 74 75 // Validate checks the generic and specific parts of the user jwt 76 func (u *UserClaims) Validate(vr *ValidationResults) { 77 u.ClaimsData.Validate(vr) 78 u.User.Validate(vr) 79 if u.IssuerAccount != "" && !nkeys.IsValidPublicAccountKey(u.IssuerAccount) { 80 vr.AddError("account_id is not an account public key") 81 } 82 } 83 84 // ExpectedPrefixes defines the types that can encode a user JWT, account 85 func (u *UserClaims) ExpectedPrefixes() []nkeys.PrefixByte { 86 return []nkeys.PrefixByte{nkeys.PrefixByteAccount} 87 } 88 89 // Claims returns the generic data from a user jwt 90 func (u *UserClaims) Claims() *ClaimsData { 91 return &u.ClaimsData 92 } 93 94 // Payload returns the user specific data from a user JWT 95 func (u *UserClaims) Payload() interface{} { 96 return &u.User 97 } 98 99 func (u *UserClaims) String() string { 100 return u.ClaimsData.String(u) 101 } 102 103 // IsBearerToken returns true if nonce-signing requirements should be skipped 104 func (u *UserClaims) IsBearerToken() bool { 105 return u.BearerToken 106 }