1 /* 2 * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * @author Aeneas Rekkas <aeneas+oss@aeneas.io> 17 * @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> 18 * @license Apache-2.0 19 * 20 */ 21 22 package jwt 23 24 // Headers is the jwt headers 25 type Headers struct { 26 Extra map[string]interface{} 27 } 28 29 func NewHeaders() *Headers { 30 return &Headers{Extra: map[string]interface{}{}} 31 } 32 33 // ToMap will transform the headers to a map structure 34 func (h *Headers) ToMap() map[string]interface{} { 35 var filter = map[string]bool{"alg": true, "typ": true} 36 var extra = map[string]interface{}{} 37 38 // filter known values from extra. 39 for k, v := range h.Extra { 40 if _, ok := filter[k]; !ok { 41 extra[k] = v 42 } 43 } 44 45 return extra 46 } 47 48 // Add will add a key-value pair to the extra field 49 func (h *Headers) Add(key string, value interface{}) { 50 if h.Extra == nil { 51 h.Extra = make(map[string]interface{}) 52 } 53 h.Extra[key] = value 54 } 55 56 // Get will get a value from the extra field based on a given key 57 func (h *Headers) Get(key string) interface{} { 58 return h.Extra[key] 59 } 60 61 // ToMapClaims will return a jwt-go MapClaims representation 62 func (h Headers) ToMapClaims() MapClaims { 63 return h.ToMap() 64 } 65