-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathauth.go
91 lines (80 loc) · 1.9 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
package auth
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/koltyakov/gosip"
"github.com/koltyakov/gosip/auth/addin"
"github.com/koltyakov/gosip/auth/adfs"
"github.com/koltyakov/gosip/auth/azurecert"
"github.com/koltyakov/gosip/auth/azurecreds"
"github.com/koltyakov/gosip/auth/device"
"github.com/koltyakov/gosip/auth/fba"
"github.com/koltyakov/gosip/auth/ntlm"
"github.com/koltyakov/gosip/auth/saml"
"github.com/koltyakov/gosip/auth/tmg"
)
// NewAuthByStrategy resolves AuthCnfg object based on strategy name
func NewAuthByStrategy(strategy string) (gosip.AuthCnfg, error) {
var auth gosip.AuthCnfg
switch strategy {
case "azurecert":
auth = &azurecert.AuthCnfg{}
break
case "azurecreds":
auth = &azurecreds.AuthCnfg{}
break
case "device":
auth = &device.AuthCnfg{}
break
case "addin":
auth = &addin.AuthCnfg{}
break
case "adfs":
auth = &adfs.AuthCnfg{}
break
case "fba":
auth = &fba.AuthCnfg{}
break
case "ntlm":
auth = &ntlm.AuthCnfg{}
break
case "saml":
auth = &saml.AuthCnfg{}
break
case "tmg":
auth = &tmg.AuthCnfg{}
break
default:
return nil, fmt.Errorf("can't resolve the strategy: %s", strategy)
}
return auth, nil
}
// NewAuthFromFile resolves AuthCnfg object based on private file
// private.json must contain "strategy" property along with strategy-specific properties
func NewAuthFromFile(privateFile string) (gosip.AuthCnfg, error) {
jsonFile, err := os.Open(privateFile)
if err != nil {
return nil, err
}
defer func() { _ = jsonFile.Close() }()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return nil, err
}
var cnfg struct {
Strategy string `json:"strategy"`
}
if err := json.Unmarshal(byteValue, &cnfg); err != nil {
return nil, err
}
auth, err := NewAuthByStrategy(cnfg.Strategy)
if err != nil {
return nil, err
}
if err := auth.ParseConfig(byteValue); err != nil {
return nil, err
}
return auth, nil
}