forked from koltyakov/gosip
/
azurecert.go
190 lines (166 loc) · 5.27 KB
/
azurecert.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
// Package azurecert implements AAD Certificate Auth Flow
// See more:
// - https://docs.microsoft.com/en-us/azure/developer/go/azure-sdk-authorization#use-file-based-authentication
//
// Amongst supported platform versions are:
// - SharePoint Online + Azure
package azurecert
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/patrickmn/go-cache"
"github.com/recolabs/gosip"
"github.com/recolabs/gosip/cpass"
)
var (
storage = cache.New(5*time.Minute, 10*time.Minute)
)
// AuthCnfg - AAD Certificate Auth Flow
/* Config sample:
{
"siteUrl": "https://contoso.sharepoint.com/sites/test",
"tenantId": "e4d43069-8ecb-49c4-8178-5bec83c53e9d",
"clientId": "628cc712-c9a4-48f0-a059-af64bdbb4be5",
"certPath": "cert.pfx",
"certPass": "password"
}
*/
type AuthCnfg struct {
SiteURL string `json:"siteUrl"` // SPSite or SPWeb URL, which is the context target for the API calls
TenantID string `json:"tenantId"` // Azure Tenant ID
ClientID string `json:"clientId"` // Azure Client ID
CertPath string `json:"certPath"` // Azure certificate (.pfx) file location, relative to config location or absolute
CertPass string `json:"certPass"` // Azure certificate export password
authorizer autorest.Authorizer
privateFile string
masterKey string
}
// ReadConfig reads private config with auth options
func (c *AuthCnfg) ReadConfig(privateFile string) error {
c.privateFile = privateFile
f, err := os.Open(privateFile)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
byteValue, _ := io.ReadAll(f)
return c.ParseConfig(byteValue)
}
// ParseConfig parses credentials from a provided JSON byte array content
func (c *AuthCnfg) ParseConfig(byteValue []byte) error {
if err := json.Unmarshal(byteValue, &c); err != nil {
return err
}
c.CertPath = path.Join(path.Dir(c.privateFile), c.CertPath)
crypt := cpass.Cpass(c.masterKey)
secret, err := crypt.Decode(c.CertPass)
if err == nil {
c.CertPass = secret
}
return nil
}
// WriteConfig writes private config with auth options
func (c *AuthCnfg) WriteConfig(privateFile string) error {
crypt := cpass.Cpass(c.masterKey)
secret, err := crypt.Encode(c.CertPass)
if err != nil {
return err
}
config := &AuthCnfg{
SiteURL: c.SiteURL,
TenantID: c.TenantID,
ClientID: c.ClientID,
CertPath: c.CertPass,
CertPass: secret,
}
file, _ := json.MarshalIndent(config, "", " ")
return os.WriteFile(privateFile, file, 0644)
}
// SetMasterkey defines custom masterkey
func (c *AuthCnfg) SetMasterkey(masterKey string) { c.masterKey = masterKey }
// GetAuth authenticates, receives access token
func (c *AuthCnfg) GetAuth(ctx context.Context) (string, int64, error) {
if c.authorizer == nil {
u, _ := url.Parse(c.SiteURL)
resource := fmt.Sprintf("https://%s", u.Host)
config := auth.NewClientCertificateConfig(c.CertPath, c.CertPass, c.ClientID, c.TenantID)
config.Resource = resource
authorizer, err := config.Authorizer()
if err != nil {
return "", 0, err
}
c.authorizer = authorizer
}
// token, err := config.ServicePrincipalToken()
// if err != nil {
// return "", 0, err
// }
// return token.Token().AccessToken, token.Token().Expires().Unix(), nil
return c.getToken(ctx)
}
// GetSiteURL gets SharePoint siteURL
func (c *AuthCnfg) GetSiteURL() string { return c.SiteURL }
// GetStrategy gets auth strategy name
func (c *AuthCnfg) GetStrategy() string { return "azurecert" }
// SetAuth authenticates request
// noinspection GoUnusedParameter
func (c *AuthCnfg) SetAuth(req *http.Request, httpClient *gosip.SPClient) error {
authToken, _, err := c.GetAuth(req.Context())
if err != nil {
return err
}
// _, err := c.authorizer.WithAuthorization()(preparer{}).Prepare(req)
req.Header.Set("Authorization", "Bearer "+authToken)
return err
}
// Getting token with prepare for external usage scenarious
func (c *AuthCnfg) getToken(ctx context.Context) (string, int64, error) {
// Get from cache
parsedURL, err := url.Parse(c.SiteURL)
if err != nil {
return "", 0, err
}
cacheKey := parsedURL.Host + "@" + c.GetStrategy() + "@" + c.TenantID + "@" + c.ClientID
if accessToken, exp, found := storage.GetWithExpiration(cacheKey); found {
return accessToken.(string), exp.Unix(), nil
}
// Get token
req, _ := http.NewRequestWithContext(ctx, "GET", c.SiteURL, nil)
req, err = c.authorizer.WithAuthorization()(preparer{}).Prepare(req)
if err != nil {
return "", 0, err
}
token := strings.Replace(req.Header.Get("Authorization"), "Bearer ", "", 1)
tt := strings.Split(token, ".")
if len(tt) != 3 {
return "", 0, fmt.Errorf("incorrect jwt")
}
jsonBytes, err := base64.RawURLEncoding.DecodeString(tt[1])
if err != nil {
return "", 0, fmt.Errorf("can't decode jwt base64 string")
}
j := struct {
Exp int64 `json:"exp"`
}{}
_ = json.Unmarshal(jsonBytes, &j)
// Save to cache
exp := time.Unix(j.Exp, 0).Add(-60 * time.Second)
storage.Set(cacheKey, token, time.Until(exp))
// fmt.Println(time.Until(exp))
return token, exp.Unix(), nil
}
// Preparer implements autorest.Preparer interface
type preparer struct{}
// Prepare satisfies autorest.Preparer interface
func (p preparer) Prepare(req *http.Request) (*http.Request, error) { return req, nil }