1// Copyright 2022 Google LLC 2// 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 17package jwt 18 19import ( 20 "fmt" 21 "time" 22) 23 24// VerifiedJWT is a verified JWT token. 25type VerifiedJWT struct { 26 token *RawJWT 27} 28 29// newVerifiedJWT generates a new VerifiedJWT 30func newVerifiedJWT(rawJWT *RawJWT) (*VerifiedJWT, error) { 31 if rawJWT == nil { 32 return nil, fmt.Errorf("rawJWT can't be nil") 33 } 34 return &VerifiedJWT{ 35 token: rawJWT, 36 }, nil 37} 38 39// JSONPayload marshals a VerifiedJWT payload to JSON. 40func (v *VerifiedJWT) JSONPayload() ([]byte, error) { 41 return v.token.JSONPayload() 42} 43 44// HasTypeHeader return whether a RawJWT contains a type header. 45func (v *VerifiedJWT) HasTypeHeader() bool { 46 return v.token.HasTypeHeader() 47} 48 49// TypeHeader returns the JWT type header. 50func (v *VerifiedJWT) TypeHeader() (string, error) { 51 return v.token.TypeHeader() 52} 53 54// HasAudiences checks whether a JWT contains the audience claim ('aud'). 55func (v *VerifiedJWT) HasAudiences() bool { 56 return v.token.HasAudiences() 57} 58 59// Audiences returns a list of audiences from the 'aud' claim. 60// If the 'aud' claim is a single string, it is converted into a list with a single entry. 61func (v *VerifiedJWT) Audiences() ([]string, error) { 62 return v.token.Audiences() 63} 64 65// HasSubject checks whether a JWT contains an issuer claim ('sub'). 66func (v *VerifiedJWT) HasSubject() bool { 67 return v.token.HasSubject() 68} 69 70// Subject returns the subject claim ('sub') or an error if no claim is present. 71func (v *VerifiedJWT) Subject() (string, error) { 72 return v.token.Subject() 73} 74 75// HasIssuer checks whether a JWT contains an issuer claim ('iss'). 76func (v *VerifiedJWT) HasIssuer() bool { 77 return v.token.HasIssuer() 78} 79 80// Issuer returns the issuer claim ('iss') or an error if no claim is present. 81func (v *VerifiedJWT) Issuer() (string, error) { 82 return v.token.Issuer() 83} 84 85// HasJWTID checks whether a JWT contains an JWT ID claim ('jti'). 86func (v *VerifiedJWT) HasJWTID() bool { 87 return v.token.HasJWTID() 88} 89 90// JWTID returns the JWT ID claim ('jti') or an error if no claim is present. 91func (v *VerifiedJWT) JWTID() (string, error) { 92 return v.token.JWTID() 93} 94 95// HasIssuedAt checks whether a JWT contains an issued at claim ('iat'). 96func (v *VerifiedJWT) HasIssuedAt() bool { 97 return v.token.HasIssuedAt() 98} 99 100// IssuedAt returns the issued at claim ('iat') or an error if no claim is present. 101func (v *VerifiedJWT) IssuedAt() (time.Time, error) { 102 return v.token.IssuedAt() 103} 104 105// HasExpiration checks whether a JWT contains an expiration time claim ('exp'). 106func (v *VerifiedJWT) HasExpiration() bool { 107 return v.token.HasExpiration() 108} 109 110// ExpiresAt returns the expiration claim ('exp') or an error if no claim is present. 111func (v *VerifiedJWT) ExpiresAt() (time.Time, error) { 112 return v.token.ExpiresAt() 113} 114 115// HasNotBefore checks whether a JWT contains a not before claim ('nbf'). 116func (v *VerifiedJWT) HasNotBefore() bool { 117 return v.token.HasNotBefore() 118} 119 120// NotBefore returns the not before claim ('nbf') or an error if no claim is present. 121func (v *VerifiedJWT) NotBefore() (time.Time, error) { 122 return v.token.NotBefore() 123} 124 125// HasStringClaim checks whether a claim of type string is present. 126func (v *VerifiedJWT) HasStringClaim(name string) bool { 127 return v.token.HasStringClaim(name) 128} 129 130// StringClaim returns a custom string claim or an error if no claim is present. 131func (v *VerifiedJWT) StringClaim(name string) (string, error) { 132 return v.token.StringClaim(name) 133} 134 135// HasNumberClaim checks whether a claim of type number is present. 136func (v *VerifiedJWT) HasNumberClaim(name string) bool { 137 return v.token.HasNumberClaim(name) 138} 139 140// NumberClaim returns a custom number claim or an error if no claim is present. 141func (v *VerifiedJWT) NumberClaim(name string) (float64, error) { 142 return v.token.NumberClaim(name) 143} 144 145// HasBooleanClaim checks whether a claim of type boolean is present. 146func (v *VerifiedJWT) HasBooleanClaim(name string) bool { 147 return v.token.HasBooleanClaim(name) 148} 149 150// BooleanClaim returns a custom bool claim or an error if no claim is present. 151func (v *VerifiedJWT) BooleanClaim(name string) (bool, error) { 152 return v.token.BooleanClaim(name) 153} 154 155// HasNullClaim checks whether a claim of type null is present. 156func (v *VerifiedJWT) HasNullClaim(name string) bool { 157 return v.token.HasNullClaim(name) 158} 159 160// HasArrayClaim checks whether a claim of type list is present. 161func (v *VerifiedJWT) HasArrayClaim(name string) bool { 162 return v.token.HasArrayClaim(name) 163} 164 165// ArrayClaim returns a slice representing a JSON array for a claim or an error if the claim is empty. 166func (v *VerifiedJWT) ArrayClaim(name string) ([]interface{}, error) { 167 return v.token.ArrayClaim(name) 168} 169 170// HasObjectClaim checks whether a claim of type JSON object is present. 171func (v *VerifiedJWT) HasObjectClaim(name string) bool { 172 return v.token.HasObjectClaim(name) 173} 174 175// ObjectClaim returns a map representing a JSON object for a claim or an error if the claim is empty. 176func (v *VerifiedJWT) ObjectClaim(name string) (map[string]interface{}, error) { 177 return v.token.ObjectClaim(name) 178} 179 180// CustomClaimNames returns a list with the name of custom claims in a VerifiedJWT. 181func (v *VerifiedJWT) CustomClaimNames() []string { 182 return v.token.CustomClaimNames() 183} 184