forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_delegation_credential.go
64 lines (52 loc) · 2.17 KB
/
user_delegation_credential.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
//go:build go1.18
// +build go1.18
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package exported
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/internal/generated"
)
// NewUserDelegationCredential creates a new UserDelegationCredential using a Storage account's Name and a user delegation Key from it
func NewUserDelegationCredential(accountName string, udk UserDelegationKey) *UserDelegationCredential {
return &UserDelegationCredential{
accountName: accountName,
userDelegationKey: udk,
}
}
// UserDelegationKey contains UserDelegationKey.
type UserDelegationKey = generated.UserDelegationKey
// UserDelegationCredential contains an account's name and its user delegation key.
type UserDelegationCredential struct {
accountName string
userDelegationKey UserDelegationKey
}
// AccountName returns the Storage account's Name
func (f *UserDelegationCredential) getAccountName() string {
return f.accountName
}
// GetUDKParams is a helper method for accessing the user delegation key parameters outside of this package.
func GetAccountName(udc *UserDelegationCredential) string {
return udc.getAccountName()
}
// computeHMACSHA256 generates a hash signature for an HTTP request or for a SAS.
func (f *UserDelegationCredential) computeHMACSHA256(message string) (string, error) {
bytes, _ := base64.StdEncoding.DecodeString(*f.userDelegationKey.Value)
h := hmac.New(sha256.New, bytes)
_, err := h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil)), err
}
// ComputeUDCHMACSHA256 is a helper method for computing the signed string outside of this package.
func ComputeUDCHMACSHA256(udc *UserDelegationCredential, message string) (string, error) {
return udc.computeHMACSHA256(message)
}
// GetUDKParams returns UserDelegationKey
func (f *UserDelegationCredential) getUDKParams() *UserDelegationKey {
return &f.userDelegationKey
}
// GetUDKParams is a helper method for accessing the user delegation key parameters outside of this package.
func GetUDKParams(udc *UserDelegationCredential) *UserDelegationKey {
return udc.getUDKParams()
}