/
auth.go
236 lines (209 loc) · 6.34 KB
/
auth.go
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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Package auth provides authentication methods.
package auth
import (
"cmp"
"encoding/binary"
"fmt"
"math"
"slices"
"github.com/SAP/go-hdb/driver/internal/protocol/encoding"
)
/*
authentication method types supported by the driver:
- basic authentication (username, password based) (whether SCRAMSHA256 or SCRAMPBKDF2SHA256) and
- X509 (client certificate) authentication and
- JWT (token) authentication
*/
const (
MtSCRAMSHA256 = "SCRAMSHA256" // password
MtSCRAMPBKDF2SHA256 = "SCRAMPBKDF2SHA256" // password pbkdf2
MtX509 = "X509" // client certificate
MtJWT = "JWT" // json web token
MtSessionCookie = "SessionCookie" // session cookie
)
// authentication method orders.
const (
MoSessionCookie byte = iota
MoX509
MoJWT
MoSCRAMPBKDF2SHA256
MoSCRAMSHA256
)
// A Method defines the interface for an authentication method.
type Method interface {
fmt.Stringer
Typ() string
Order() byte
PrepareInitReq(prms *Prms) error
InitRepDecode(d *Decoder) error
PrepareFinalReq(prms *Prms) error
FinalRepDecode(d *Decoder) error
}
// Methods defines a collection of methods.
type Methods map[string]Method // key equals authentication method type.
// Order returns an ordered method slice.
func (m Methods) Order() []Method {
methods := make([]Method, 0, len(m))
for _, e := range m {
methods = append(methods, e)
}
slices.SortFunc(methods, func(m1, m2 Method) int { return cmp.Compare(m1.Order(), m2.Order()) })
return methods
}
// CookieGetter is implemented by authentication methods supporting cookies to reconnect.
type CookieGetter interface {
Cookie() (logonname string, cookie []byte)
}
var (
_ Method = (*SCRAMSHA256)(nil)
_ Method = (*SCRAMPBKDF2SHA256)(nil)
_ Method = (*JWT)(nil)
_ Method = (*X509)(nil)
_ Method = (*SessionCookie)(nil)
)
// subPrmsSize is the type used to encode and decode the size of sub parameters.
// The hana protocoll supports whether:
// - a size <= 245 encoded in one byte or
// - an unsigned 2 byte integer size encoded in three bytes
// . first byte equals 255
// . second and third byte is an big endian encoded uint16
type subPrmsSize int
const (
maxSubPrmsSize1ByteLen = 245
subPrmsSize2ByteIndicator = 255
)
func (s subPrmsSize) fieldSize() int {
if s > maxSubPrmsSize1ByteLen {
return 3
}
return 1
}
func (s subPrmsSize) encode(e *encoding.Encoder) error {
switch {
case s <= maxSubPrmsSize1ByteLen:
e.Byte(byte(s))
case s <= math.MaxUint16:
e.Byte(subPrmsSize2ByteIndicator)
e.Uint16ByteOrder(uint16(s), binary.BigEndian) // big endian
default:
return fmt.Errorf("invalid subparameter size %d - maximum %d", s, 42)
}
return nil
}
func (s *subPrmsSize) decode(d *encoding.Decoder) {
b := d.Byte()
switch {
case b <= maxSubPrmsSize1ByteLen:
*s = subPrmsSize(b)
case b == subPrmsSize2ByteIndicator:
*s = subPrmsSize(d.Uint16ByteOrder(binary.BigEndian))
default:
panic(fmt.Sprintf("invalid sub parameter size indicator %d", b))
}
}
// Decoder represents an authentication decoder.
type Decoder struct {
d *encoding.Decoder
}
// NewDecoder returns a new decoder instance.
func NewDecoder(d *encoding.Decoder) *Decoder {
return &Decoder{d: d}
}
// NumPrm ckecks the number of parameters and returns an error if not equal expected, nil otherwise.
func (d *Decoder) NumPrm(expected int) error {
numPrm := int(d.d.Int16())
if numPrm != expected {
return fmt.Errorf("invalid number of parameters %d - expected %d", numPrm, expected)
}
return nil
}
func (d *Decoder) String() string { _, s := d.d.LIString(); return s }
func (d *Decoder) cesu8String() (string, error) { _, s, err := d.d.CESU8LIString(); return s, err }
func (d *Decoder) bytes() []byte { _, b := d.d.LIBytes(); return b }
func (d *Decoder) bigUint32() (uint32, error) {
size := d.d.Byte()
if size != encoding.IntegerFieldSize { // 4 bytes
return 0, fmt.Errorf("invalid auth uint32 size %d - expected %d", size, encoding.IntegerFieldSize)
}
return d.d.Uint32ByteOrder(binary.BigEndian), nil // big endian coded (e.g. rounds param)
}
func (d *Decoder) subSize() int {
var subSize subPrmsSize
(&subSize).decode(d.d)
return int(subSize)
}
// Prms represents authentication parameters.
type Prms struct {
prms []any
}
func (p *Prms) String() string { return fmt.Sprintf("%v", p.prms) }
// AddCESU8String adds a CESU8 string parameter.
func (p *Prms) AddCESU8String(s string) { p.prms = append(p.prms, s) } // unicode string
func (p *Prms) addEmpty() { p.prms = append(p.prms, []byte{}) }
func (p *Prms) addBytes(b []byte) { p.prms = append(p.prms, b) }
func (p *Prms) addString(s string) { p.prms = append(p.prms, []byte(s)) } // treat like bytes to distinguisch from unicode string
func (p *Prms) addPrms() *Prms {
prms := &Prms{}
p.prms = append(p.prms, prms)
return prms
}
// Size returns the size in bytes of the parameters.
func (p *Prms) Size() int {
size := encoding.SmallintFieldSize // no of parameters (2 bytes)
for _, e := range p.prms {
switch e := e.(type) {
case []byte, string:
size += encoding.VarFieldSize(e)
case *Prms:
subSize := subPrmsSize(e.Size())
size += (int(subSize) + subSize.fieldSize())
default:
panic(fmt.Sprintf("invalid parameter %[1]v %[1]t", e)) // should not happen
}
}
return size
}
// Encode encodes the parameters.
func (p *Prms) Encode(enc *encoding.Encoder) error {
numPrms := len(p.prms)
if numPrms > math.MaxInt16 {
return fmt.Errorf("invalid number of parameters %d - maximum %d", numPrms, math.MaxInt16)
}
enc.Int16(int16(numPrms))
for _, e := range p.prms {
switch e := e.(type) {
case []byte:
if err := enc.LIBytes(e); err != nil {
return err
}
case string:
if err := enc.CESU8LIString(e); err != nil {
return err
}
case *Prms:
subSize := subPrmsSize(e.Size())
if err := subSize.encode(enc); err != nil {
return err
}
if err := e.Encode(enc); err != nil {
return err
}
default:
panic(fmt.Sprintf("invalid parameter %[1]v %[1]t", e)) // should not happen
}
}
return nil
}
// Decode decodes the parameters.
func (p *Prms) Decode(dec *encoding.Decoder) error {
numPrms := int(dec.Int16())
for i := 0; i < numPrms; i++ {
}
return nil
}
func checkAuthMethodType(mt, expected string) error {
if mt != expected {
return fmt.Errorf("invalid method %s - expected %s", mt, expected)
}
return nil
}