Debian Codegraph @

storage/internal/auth/authorizer_shared_key_lite_table.go

raw symbols SCIP
1 package auth
2
3 import (
4 "net/http"
5 "strings"
6
7 "github.com/Azure/go-autorest/autorest"
8 )
9
10 // SharedKeyLiteTableAuthorizer implements an authorization for Shared Key Lite
11 // this can be used for interaction with Table Storage Endpoints
12 type SharedKeyLiteTableAuthorizer struct {
13 storageAccountName string
14 storageAccountKey string
15 }
16
17 // NewSharedKeyLiteAuthorizer crates a SharedKeyLiteAuthorizer using the given credentials
18 func NewSharedKeyLiteTableAuthorizer(accountName, accountKey string) *SharedKeyLiteTableAuthorizer {
19 return &SharedKeyLiteTableAuthorizer{
20 storageAccountName: accountName,
21 storageAccountKey: accountKey,
22 }
23 }
24
25 // WithAuthorization returns a PrepareDecorator that adds an HTTP Authorization header whose
26 // value is "SharedKeyLite " followed by the computed key.
27 // This can be used for the Blob, Queue, and File Services
28 //
29 // from: https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key
30 // You may use Shared Key Lite authorization to authorize a request made against the
31 // 2009-09-19 version and later of the Blob and Queue services,
32 // and version 2014-02-14 and later of the File services.
33 func (skl *SharedKeyLiteTableAuthorizer) WithAuthorization() autorest.PrepareDecorator {
34 return func(p autorest.Preparer) autorest.Preparer {
35 return autorest.PreparerFunc(func(r *http.Request) (*http.Request, error) {
36 r, err := p.Prepare(r)
37 if err != nil {
38 return r, err
39 }
40
41 key, err := buildSharedKeyLiteTable(skl.storageAccountName, skl.storageAccountKey, r)
42 if err != nil {
43 return r, err
44 }
45
46 sharedKeyHeader := formatSharedKeyLiteAuthorizationHeader(skl.storageAccountName, *key)
47 return autorest.Prepare(r, autorest.WithHeader(HeaderAuthorization, sharedKeyHeader))
48 })
49 }
50 }
51
52 func buildSharedKeyLiteTable(accountName, storageAccountKey string, r *http.Request) (*string, error) {
53 // first ensure the relevant headers are configured
54 prepareHeadersForRequest(r)
55
56 sharedKey, err := computeSharedKeyLiteTable(r.URL.String(), accountName, r.Header)
57 if err != nil {
58 return nil, err
59 }
60
61 // we then need to HMAC that value
62 hmacdValue := hmacValue(storageAccountKey, *sharedKey)
63 return &hmacdValue, nil
64 }
65
66 // computeSharedKeyLite computes the Shared Key Lite required for Storage Authentication
67 // NOTE: this function assumes that the `x-ms-date` field is set
68 func computeSharedKeyLiteTable(url string, accountName string, headers http.Header) (*string, error) {
69 dateHeader := headers.Get("x-ms-date")
70 canonicalizedResource, err := buildCanonicalizedResource(url, accountName)
71 if err != nil {
72 return nil, err
73 }
74
75 canonicalizedString := buildCanonicalizedStringForSharedKeyLiteTable(*canonicalizedResource, dateHeader)
76 return &canonicalizedString, nil
77 }
78
79 func buildCanonicalizedStringForSharedKeyLiteTable(canonicalizedResource, dateHeader string) string {
80 return strings.Join([]string{
81 dateHeader,
82 canonicalizedResource,
83 }, "\n")
84 }