...

Source file src/github.com/ory/fosite/token/jwt/claims.go

Documentation: github.com/ory/fosite/token/jwt

     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  import "time"
    25  
    26  // Mapper is the interface used internally to map key-value pairs
    27  type Mapper interface {
    28  	ToMap() map[string]interface{}
    29  	Add(key string, value interface{})
    30  	Get(key string) interface{}
    31  }
    32  
    33  // ToString will return a string representation of a map
    34  func ToString(i interface{}) string {
    35  	if i == nil {
    36  		return ""
    37  	}
    38  
    39  	if s, ok := i.(string); ok {
    40  		return s
    41  	}
    42  
    43  	if sl, ok := i.([]string); ok {
    44  		if len(sl) == 1 {
    45  			return sl[0]
    46  		}
    47  	}
    48  
    49  	return ""
    50  }
    51  
    52  // ToTime will try to convert a given input to a time.Time structure
    53  func ToTime(i interface{}) time.Time {
    54  	if i == nil {
    55  		return time.Time{}
    56  	}
    57  
    58  	if t, ok := i.(int64); ok {
    59  		return time.Unix(t, 0).UTC()
    60  	} else if t, ok := i.(float64); ok {
    61  		return time.Unix(int64(t), 0).UTC()
    62  	} else if t, ok := i.(time.Time); ok {
    63  		return t
    64  	}
    65  
    66  	return time.Time{}
    67  }
    68  
    69  // Filter will filter out elements based on keys in a given input map na key-slice
    70  func Filter(elements map[string]interface{}, keys ...string) map[string]interface{} {
    71  	var keyIdx = make(map[string]bool)
    72  	var result = make(map[string]interface{})
    73  
    74  	for _, key := range keys {
    75  		keyIdx[key] = true
    76  	}
    77  
    78  	for k, e := range elements {
    79  		if _, ok := keyIdx[k]; !ok {
    80  			result[k] = e
    81  		}
    82  	}
    83  
    84  	return result
    85  }
    86  
    87  // Copy will copy all elements in a map and return a new representational map
    88  func Copy(elements map[string]interface{}) (result map[string]interface{}) {
    89  	result = make(map[string]interface{}, len(elements))
    90  	for k, v := range elements {
    91  		result[k] = v
    92  	}
    93  
    94  	return result
    95  }
    96  

View as plain text