| 1 | package auth |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | |
| 7 | "github.com/Azure/go-autorest/autorest" |
| 8 | ) |
| 9 | |
| 10 | // SharedKeyLiteAuthorizer implements an authorization for Shared Key Lite |
| 11 | // this can be used for interaction with Blob, File and Queue Storage Endpoints |
| 12 | type SharedKeyLiteAuthorizer struct { |
| 13 | storageAccountName string |
| 14 | storageAccountKey string |
| 15 | } |
| 16 | |
| 17 | // NewSharedKeyLiteAuthorizer crates a SharedKeyLiteAuthorizer using the given credentials |
| 18 | func NewSharedKeyLiteAuthorizer(accountName, accountKey string) *SharedKeyLiteAuthorizer { |
| 19 | return &SharedKeyLiteAuthorizer{ |
| 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 *SharedKeyLiteAuthorizer) 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 := buildSharedKeyLite(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 | func buildSharedKeyLite(accountName, storageAccountKey string, r *http.Request) (*string, error) { |
| 52 | // first ensure the relevant headers are configured |
| 53 | prepareHeadersForRequest(r) |
| 54 | |
| 55 | sharedKey, err := computeSharedKeyLite(r.Method, r.URL.String(), accountName, r.Header) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | // we then need to HMAC that value |
| 61 | hmacdValue := hmacValue(storageAccountKey, *sharedKey) |
| 62 | return &hmacdValue, nil |
| 63 | } |
| 64 | |
| 65 | // computeSharedKeyLite computes the Shared Key Lite required for Storage Authentication |
| 66 | // NOTE: this function assumes that the `x-ms-date` field is set |
| 67 | func computeSharedKeyLite(verb, url string, accountName string, headers http.Header) (*string, error) { |
| 68 | canonicalizedResource, err := buildCanonicalizedResource(url, accountName) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | canonicalizedHeaders := buildCanonicalizedHeader(headers) |
| 74 | canonicalizedString := buildCanonicalizedStringForSharedKeyLite(verb, headers, canonicalizedHeaders, *canonicalizedResource) |
| 75 | return &canonicalizedString, nil |
| 76 | } |
| 77 | |
| 78 | func buildCanonicalizedStringForSharedKeyLite(verb string, headers http.Header, canonicalizedHeaders, canonicalizedResource string) string { |
| 79 | return strings.Join([]string{ |
| 80 | verb, |
| 81 | headers.Get(HeaderContentMD5), // TODO: this appears to always be empty? |
| 82 | headers.Get(HeaderContentType), |
| 83 | "", // date should be nil, apparently :shrug: |
| 84 | canonicalizedHeaders, |
| 85 | canonicalizedResource, |
| 86 | }, "\n") |
| 87 | } |