-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.go
79 lines (63 loc) · 2.17 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
package confluence
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2022 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"encoding/base64"
"errors"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Auth is interface for authorization method
type Auth interface {
Validate() error
Encode() string
}
// ////////////////////////////////////////////////////////////////////////////////// //
// AuthBasic is struct with data for basic authorization
type AuthBasic struct {
User string
Password string
}
// AuthToken is struct with data for personal token authorization
type AuthToken struct {
Token string
}
// ////////////////////////////////////////////////////////////////////////////////// //
var (
ErrEmptyUser = errors.New("User can't be empty")
ErrEmptyPassword = errors.New("Password can't be empty")
ErrEmptyToken = errors.New("Token can't be empty")
ErrTokenWrongLength = errors.New("Token length must be equal to 44")
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Validate validates authorization data
func (a AuthBasic) Validate() error {
switch {
case a.User == "":
return ErrEmptyUser
case a.Password == "":
return ErrEmptyPassword
}
return nil
}
// Encode encodes data for authorization
func (a AuthBasic) Encode() string {
return "Basic " + base64.StdEncoding.EncodeToString([]byte(a.User+":"+a.Password))
}
// Validate validates authorization data
func (a AuthToken) Validate() error {
switch {
case a.Token == "":
return ErrEmptyToken
case len(a.Token) != 44:
return ErrTokenWrongLength
}
return nil
}
// Encode encodes data for authorization
func (a AuthToken) Encode() string {
return "Bearer " + a.Token
}