Implement authentication

This commit is contained in:
Pijus Kamandulis
2024-02-21 23:40:54 +02:00
parent 790192bf5a
commit 6a40492c7b
11 changed files with 227 additions and 3 deletions

View File

@@ -0,0 +1,26 @@
package authentication
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"strings"
)
// https://learn.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources
func GenerateSignature(verb string, resourceType string, resourceId string, date string, masterKey string) string {
payload := fmt.Sprintf(
"%s\n%s\n%s\n%s\n%s\n",
strings.ToLower(verb),
strings.ToLower(resourceType),
resourceId,
strings.ToLower(date),
"")
masterKeyBytes, _ := base64.StdEncoding.DecodeString(masterKey)
hash := hmac.New(sha256.New, masterKeyBytes)
hash.Write([]byte(payload))
signature := base64.StdEncoding.EncodeToString(hash.Sum(nil))
return signature
}