-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathgosip.go
More file actions
291 lines (257 loc) · 8.96 KB
/
gosip.go
File metadata and controls
291 lines (257 loc) · 8.96 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
Package gosip is pure Go library for dealing with SharePoint unattended authentication and API consumption.
It supports a variety of different authentication strategies such as:
- SAML based with user credentials
- Add-in only permissions
- Azure AD auth strategies (via extensions https://go.spflow.com/auth/custom-auth)
- ADFS user credentials
- NTLM/NTLM v2 windows auth
- Auth to SharePoint behind a reverse proxy (TMG, WAP)
- Form-based authentication (FBA)
- Web login/On-Demand auth (via extension https://go.spflow.com/auth/custom-auth/on-demand)
Amongst supported platform versions are:
- SharePoint Online (SPO)
- On-Premise: 2019, 2016, and 2013
*/
package gosip
import (
"bytes"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
const version = "1.0.0"
// Default header values (exported for internal reuse across packages)
const (
// DefaultAccept is the generic JSON Accept used when callers didn't specify one
DefaultAccept = "application/json"
// DefaultAcceptVerbose is the SP2013-compatible verbose JSON Accept used by API helpers
DefaultAcceptVerbose = "application/json;odata=verbose"
// DefaultContentTypeVerbose is the verbose JSON Content-Type used by API helpers
DefaultContentTypeVerbose = "application/json;odata=verbose;charset=utf-8"
)
// AuthCnfg is an abstract auth config interface,
// allows different authentications strategies' dependency injection
type AuthCnfg interface {
// GetAuth Authentication initializer (token/cookie/header, expiration, error)
// to support capability for exposing tokens for external tools
// e.g. as of this sample project https://github.com/koltyakov/spvault
GetAuth() (string, int64, error)
// SetAuth Authentication middleware fabric
// applies round tripper or enriches requests with authentication and metadata
SetAuth(req *http.Request, client *SPClient) error
ParseConfig(jsonConf []byte) error // Parses credentials from a provided JSON byte array content
ReadConfig(configPath string) error // Reads credentials from storage
GetSiteURL() string // SiteURL getter method
GetStrategy() string // Strategy code getter
// WriteConfig(configPath string) error // Writes credential to storage // considering remove the method from interface
}
// SPClient : SharePoint HTTP client struct
type SPClient struct {
http.Client
AuthCnfg AuthCnfg // authentication configuration interface
ConfigPath string // private.json location path, optional when AuthCnfg is provided with creds explicitly
RetryPolicies map[int]int // allows redefining error state requests retry policies
Hooks *HookHandlers // hook handlers definition
}
// SPError represents a SharePoint HTTP error with status code and body
type SPError struct {
StatusCode int
Status string
Body string
}
func (e *SPError) Error() string {
// Preserve existing error message format
return fmt.Sprintf("%s :: %s", e.Status, e.Body)
}
// Execute : SharePoint HTTP client
// is a wrapper for standard http.Client' `Do` method, injects authorization tokens, etc.
func (c *SPClient) Execute(req *http.Request) (*http.Response, error) {
// Prepare a safe body replay strategy before attempts begin
const maxReplayBytes int64 = 10 << 20 // 10MB cap for in-memory buffering
var bodyRebuilder func() (io.ReadCloser, error)
// Prefer existing GetBody if provided by caller/new request constructors
if req.GetBody != nil {
bodyRebuilder = req.GetBody
} else if req.Body != nil {
// Pre-buffer small bodies when content length is known and under cap
if req.ContentLength >= 0 && req.ContentLength <= maxReplayBytes {
buf, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
// Seed the request body for the first attempt
req.Body = io.NopCloser(bytes.NewReader(buf))
// Provide a GetBody for future attempts
req.GetBody = func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(buf)), nil
}
bodyRebuilder = req.GetBody
}
// else: unknown/large bodies fallback to per-attempt TeeReader buffering
}
for {
reqTime := time.Now()
// Apply authentication flow
if res, err := c.applyAuth(req); err != nil {
c.onError(req, reqTime, 0, err)
return res, err
}
// Setup request default headers
if err := c.applyHeaders(req); err != nil {
// An error might occur only when calling for the digest
res := &http.Response{
Status: "400 Bad Request",
StatusCode: 400,
Request: req,
}
c.onError(req, reqTime, 0, err)
return res, err
}
c.onRequest(req, reqTime, 0, nil)
reqTime = time.Now() // update request time to exclude auth-related timings
// Prepare body for this attempt
var bodyBuf bytes.Buffer
usedTee := false
if bodyRebuilder != nil {
rc, err := bodyRebuilder()
if err != nil {
c.onError(req, reqTime, 0, err)
return nil, err
}
req.Body = rc
} else if req.Body != nil {
// Fallback: tee to buffer what we read this attempt
tee := io.TeeReader(req.Body, &bodyBuf)
req.Body = io.NopCloser(tee)
usedTee = true
}
// Sending actual request to SharePoint API/resource
resp, err := c.Do(req)
if err != nil {
// Retry only for NTLM
if c.AuthCnfg.GetStrategy() == "ntlm" && c.shouldRetry(req, resp, 5) {
statusCode := 400
if resp != nil {
statusCode = resp.StatusCode
}
c.onRetry(req, reqTime, statusCode, nil)
// Reset body for next attempt
if bodyRebuilder != nil {
if rc, e := bodyRebuilder(); e == nil {
req.Body = rc
} else {
return resp, e
}
} else if usedTee {
req.Body = io.NopCloser(bytes.NewReader(bodyBuf.Bytes()))
}
continue
}
c.onError(req, reqTime, 0, err)
return resp, err
}
// Wait and retry after a delay for error state responses, due to retry policies
if retries := c.getRetryPolicy(resp.StatusCode); retries > 0 {
// Register retry in OnError hook for throttling
if resp.StatusCode == 429 {
noRetry := req.Header.Get("X-Gosip-NoRetry")
retry, _ := strconv.Atoi(req.Header.Get("X-Gosip-Retry"))
if retry < retries && noRetry != "true" {
c.onError(req, reqTime, resp.StatusCode, nil)
}
}
// When it should, shouldRetry not only checks but waits before a retry
if c.shouldRetry(req, resp, retries) {
c.onRetry(req, reqTime, resp.StatusCode, nil)
// Reset body for next attempt
if bodyRebuilder != nil {
if rc, e := bodyRebuilder(); e == nil {
req.Body = rc
} else {
return resp, e
}
} else if usedTee {
req.Body = io.NopCloser(bytes.NewReader(bodyBuf.Bytes()))
}
continue
}
}
// Return meaningful error message
var outErr error
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
var buf bytes.Buffer
tee := io.TeeReader(resp.Body, &buf)
details, _ := io.ReadAll(tee)
bodyText := string(details)
// Unescape unicode-escaped error messages for non Latin languages
if unescaped, e := strconv.Unquote("\"" + strings.Replace(bodyText, "\"", "\\\"", -1) + "\""); e == nil {
bodyText = unescaped
}
outErr = &SPError{StatusCode: resp.StatusCode, Status: resp.Status, Body: bodyText}
resp.Body = io.NopCloser(&buf)
c.onError(req, reqTime, resp.StatusCode, outErr)
}
c.onResponse(req, reqTime, resp.StatusCode, outErr)
return resp, outErr
}
}
// applyAuth applies authentication flow
func (c *SPClient) applyAuth(req *http.Request) (*http.Response, error) {
// Read stored credentials and config
if c.ConfigPath != "" && c.AuthCnfg.GetSiteURL() == "" {
_ = c.AuthCnfg.ReadConfig(c.ConfigPath)
}
// Can't resolve context siteURL
if c.AuthCnfg.GetSiteURL() == "" {
res := &http.Response{
Status: "400 Bad Request",
StatusCode: 400,
Request: req,
}
return res, fmt.Errorf("client initialization error, no siteUrl is provided")
}
// Wrap SharePoint authentication
err := c.AuthCnfg.SetAuth(req, c)
if err != nil {
res := &http.Response{
Status: "401 Unauthorized",
StatusCode: 401,
Request: req,
}
return res, err
}
return nil, nil
}
// applyHeaders patches request readers for SP API defaults
func (c *SPClient) applyHeaders(req *http.Request) error {
// Inject X-RequestDigest header when needed
digestIsRequired := (req.Method == "POST" || req.Method == "PATCH" || req.Method == "MERGE") &&
!strings.Contains(strings.ToLower(req.URL.Path), "/_api/contextinfo") &&
req.Header.Get("X-RequestDigest") == ""
if digestIsRequired {
digest, err := GetDigest(req.Context(), c)
if err != nil {
return err
}
req.Header.Set("X-RequestDigest", digest)
}
// Default SP REST API headers
if req.Header.Get("Accept") == "" {
req.Header.Set("Accept", DefaultAccept)
}
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", DefaultContentTypeVerbose)
}
// Vendor/client header
if req.Header.Get("X-ClientService-ClientTag") == "" {
req.Header.Set("X-ClientService-ClientTag", fmt.Sprintf("Gosip:@%s", version))
}
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", fmt.Sprintf("NONISV|Go|Gosip/@%s", version))
}
return nil
}