Newer
Older
package utils
import (
"fmt"
"strings"
"github.com/fatih/structs"
"github.com/oidc-mytoken/utils/utils/issuerutils"
"github.com/oidc-mytoken/server/internal/utils/hashutils"
)
// CreateMytokenSubject creates the subject of a Mytoken from the oidc subject and oidc issuer
func CreateMytokenSubject(oidcSub, oidcIss string) string {
comb := issuerutils.CombineSubIss(oidcSub, oidcIss)
return hashutils.SHA3_256Str([]byte(comb))
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// CompareNullableIntsWithNilAsInfinity compare two *int64 and handles nil as infinity. It returns 0 if both are equal,
// a positive value if a is greater than b, a negative value is a is less than b
func CompareNullableIntsWithNilAsInfinity(a, b *int64) int {
if a == nil && b == nil {
return 0
}
if a == nil { // b!=nil
return 1
}
if b == nil { // a!=nil
return -1
}
// a and b != nil
if *a == *b {
return 0
} else if *a > *b {
return 1
} else {
return -1
}
}
// SplitIgnoreEmpty splits a string at the specified delimiter without generating empty parts
func SplitIgnoreEmpty(s, del string) (ret []string) {
tmp := strings.Split(s, del)
for _, ss := range tmp {
if ss != "" {
ret = append(ret, ss)
}
}
return
}
// RSplitN splits a string s at the delimiter del into n pieces. Unlike strings.SplitN RSplitN splits the string
// starting from the right side
func RSplitN(s, del string, n int) []string {
if n == 0 {
return nil
}
if del == "" {
return nil
}
if n < 0 {
return strings.Split(s, del)
}
split := make([]string, n)
delLen := len(del)
n--
for n > 0 {
m := strings.LastIndex(s, del)
if m < 0 {
break
}
split[n] = s[m+delLen:]
s = s[:m+delLen-1]
n--
}
split[n] = s
return split[n:]
}
// StructToStringMap creates a string map from an interface{} using the passed tag name
func StructToStringMap(st interface{}, tag string) map[string]string {
s := structs.New(st)
s.TagName = tag
m := make(map[string]string)
for k, v := range s.Map() {
var str string
switch v := v.(type) {
case string:
str = v
default:
str = fmt.Sprintf("%v", v)
}
m[k] = str
}
return m
}
// StructToStringMapUsingJSONTags creates a string map from an interface{} using json tags
func StructToStringMapUsingJSONTags(st interface{}) map[string]string {
return StructToStringMap(st, "json")
}
// MinInt returns the smallest of the passed integers
func MinInt(a int, ints ...int) int {
min := a
for _, i := range ints {
if i < min {
min = i
}
}
return min
}
// MinInt64 returns the smallest of the passed integers
func MinInt64(a int64, ints ...int64) int64 {
min := a
for _, i := range ints {
if i < min {
min = i
}
}
return min
}
// ORErrors returns the first passed error that is not nil
func ORErrors(errs ...error) error {
for _, err := range errs {
if err != nil {
return err
}
}
return nil
}
// OR logically ORs multiple bools
func OR(bools ...bool) bool {
for _, b := range bools {
if b {
return b
}
}
return false
}