File: key_type_gen.go

package info (click to toggle)
golang-github-lestrrat-go-jwx 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,872 kB
  • sloc: sh: 222; makefile: 86; perl: 62
file content (106 lines) | stat: -rw-r--r-- 2,537 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Code generated by tools/cmd/genjwa/main.go. DO NOT EDIT.

package jwa

import (
	"fmt"
	"sort"
	"sync"
)

// KeyType represents the key type ("kty") that are supported
type KeyType string

// Supported values for KeyType
const (
	EC             KeyType = "EC"  // Elliptic Curve
	InvalidKeyType KeyType = ""    // Invalid KeyType
	OKP            KeyType = "OKP" // Octet string key pairs
	OctetSeq       KeyType = "oct" // Octet sequence (used to represent symmetric keys)
	RSA            KeyType = "RSA" // RSA
)

var muKeyTypes sync.RWMutex
var allKeyTypes map[KeyType]struct{}
var listKeyType []KeyType

func init() {
	muKeyTypes.Lock()
	defer muKeyTypes.Unlock()
	allKeyTypes = make(map[KeyType]struct{})
	allKeyTypes[EC] = struct{}{}
	allKeyTypes[OKP] = struct{}{}
	allKeyTypes[OctetSeq] = struct{}{}
	allKeyTypes[RSA] = struct{}{}
	rebuildKeyType()
}

// RegisterKeyType registers a new KeyType so that the jwx can properly handle the new value.
// Duplicates will silently be ignored
func RegisterKeyType(v KeyType) {
	muKeyTypes.Lock()
	defer muKeyTypes.Unlock()
	if _, ok := allKeyTypes[v]; !ok {
		allKeyTypes[v] = struct{}{}
		rebuildKeyType()
	}
}

// UnregisterKeyType unregisters a KeyType from its known database.
// Non-existent entries will silently be ignored
func UnregisterKeyType(v KeyType) {
	muKeyTypes.Lock()
	defer muKeyTypes.Unlock()
	if _, ok := allKeyTypes[v]; ok {
		delete(allKeyTypes, v)
		rebuildKeyType()
	}
}

func rebuildKeyType() {
	listKeyType = make([]KeyType, 0, len(allKeyTypes))
	for v := range allKeyTypes {
		listKeyType = append(listKeyType, v)
	}
	sort.Slice(listKeyType, func(i, j int) bool {
		return string(listKeyType[i]) < string(listKeyType[j])
	})
}

// KeyTypes returns a list of all available values for KeyType
func KeyTypes() []KeyType {
	muKeyTypes.RLock()
	defer muKeyTypes.RUnlock()
	return listKeyType
}

// Accept is used when conversion from values given by
// outside sources (such as JSON payloads) is required
func (v *KeyType) Accept(value interface{}) error {
	var tmp KeyType
	if x, ok := value.(KeyType); ok {
		tmp = x
	} else {
		var s string
		switch x := value.(type) {
		case fmt.Stringer:
			s = x.String()
		case string:
			s = x
		default:
			return fmt.Errorf(`invalid type for jwa.KeyType: %T`, value)
		}
		tmp = KeyType(s)
	}
	if _, ok := allKeyTypes[tmp]; !ok {
		return fmt.Errorf(`invalid jwa.KeyType value`)
	}

	*v = tmp
	return nil
}

// String returns the string representation of a KeyType
func (v KeyType) String() string {
	return string(v)
}