Debian Codegraph @

openid2/extensions.go

raw symbols SCIP
1 package openid2
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 type Extension struct {
9 Namespace string
10 Prefix string
11 Params map[string]string
12 }
13
14 func parseExtensions(params map[string]string) ([]Extension, error) {
15 prefixes := make(map[string]string)
16 namespaces := make(map[string]string)
17 for k, v := range params {
18 parts := strings.SplitN(k, ".", 2)
19 if len(parts) < 2 {
20 continue
21 }
22 if parts[0] != "ns" {
23 continue
24 }
25 prefix := parts[1]
26 if bannedPrefixes[prefix] {
27 return nil, fmt.Errorf("namespace prefix %q not allowed", prefix)
28 }
29 if ns, ok := prefixes[prefix]; ok && ns != v {
30 return nil, fmt.Errorf("namespace prefix %q assigned to multiple namespaces", prefix)
31 }
32 ns := v
33 if p, ok := namespaces[ns]; ok && p != prefix {
34 return nil, fmt.Errorf("namespace %q assigned to multiple prefixes", ns)
35 }
36 namespaces[ns] = prefix
37 prefixes[prefix] = ns
38 }
39 extensions := make([]Extension, len(prefixes))
40 positions := make(map[string]int)
41 i := 0
42 for p, ns := range prefixes {
43 extensions[i] = Extension{
44 Namespace: ns,
45 Prefix: p,
46 Params: map[string]string{},
47 }
48 positions[p] = i
49 i++
50 }
51 for k, v := range params {
52 parts := strings.SplitN(k, ".", 2)
53 if len(parts) < 2 {
54 continue
55 }
56 if parts[0] == "ns" {
57 continue
58 }
59 prefix := parts[0]
60 key := parts[1]
61 pos, ok := positions[prefix]
62 if !ok {
63 continue
64 }
65 extensions[pos].Params[key] = v
66 }
67 return extensions, nil
68 }
69
70 func encodeExtensions(params map[string]string, extensions []Extension) (signed []string) {
71 var i int
72 used := map[string]bool{}
73 for _, ext := range extensions {
74 prefix := ext.Prefix
75 for bannedPrefixes[prefix] || used[prefix] {
76 prefix = fmt.Sprintf("ext%d", i)
77 i++
78 }
79 used[prefix] = true
80 params["ns."+prefix] = ext.Namespace
81 for k, v := range ext.Params {
82 key := fmt.Sprintf("%s.%s", prefix, k)
83 params[key] = v
84 signed = append(signed, key)
85 }
86 }
87 return
88 }
89
90 var bannedPrefixes = map[string]bool{
91 "assoc_handle": true,
92 "assoc_type": true,
93 "claimed_id": true,
94 "contact": true,
95 "delegate": true,
96 "dh_consumer_public": true,
97 "dh_gen": true,
98 "dh_modulus": true,
99 "error": true,
100 "identity": true,
101 "invalidate_handle": true,
102 "mode": true,
103 "ns": true,
104 "op_endpoint": true,
105 "openid": true,
106 "realm": true,
107 "reference": true,
108 "response_nonce": true,
109 "return_to": true,
110 "server": true,
111 "session_type": true,
112 "sig": true,
113 "signed": true,
114 "trust_root": true,
115 }