-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4b3cac4
Showing
17 changed files
with
870 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ACS_ENDPOINT="__CHANGE_ME__" | ||
ACS_ACCESS_KEY="__CHANGE_ME__" | ||
TO_ADDRESS="__CHANGE_ME__" | ||
CC_ADDRESS="__CHANGE_ME__" | ||
FROM_ADDRESS="__CHANGE_ME__" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Created by https://www.toptal.com/developers/gitignore/api/go | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=go | ||
|
||
### Go ### | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/go | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# ================================================================= | ||
# An opinionated config for linting Go code with golangci-lint | ||
# See https://golangci-lint.run/usage/linters | ||
# ================================================================= | ||
|
||
linters: | ||
enable: | ||
- revive # Replacement for golint | ||
- gofmt # Runs gofmt as part of the linter | ||
- gosec # Find security issues | ||
- wsl # Whitespace style enforcer, a matter of taste | ||
- stylecheck # A few Go style rules | ||
- misspell # Find misspelled words | ||
- cyclop # Find cyclomatic complexity | ||
- gocyclo # Also find cyclomatic complexity | ||
- bodyclose # Check for HTTP body close errors | ||
- nilerr # Find bad nil/err handling | ||
- nilnil # Also find bad nil/err handling | ||
- gomnd # Find magic numbers, enable at your peril | ||
|
||
linters-settings: | ||
misspell: | ||
locale: UK # Enable UK spelling | ||
|
||
revive: | ||
severity: error | ||
enable-all-rules: false | ||
confidence: 0.5 | ||
rules: | ||
# There are MANY rules you could enable... | ||
# See https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md | ||
# And https://golangci-lint.run/usage/linters/#revive | ||
- name: line-length-limit | ||
severity: error | ||
arguments: [160] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Welcome | ||
|
||
Hello! Thanks for taking an interest in this project and code :) | ||
|
||
Contributions to this project are welcome of course, otherwise it wouldn't reside on GitHub 😃 however there's a few things to be aware of: | ||
|
||
- This is a personal project, it is not maintained by a team or group. | ||
- It might take a long time for the maintainer(s) to reply to issues or review PRs, they will have have a day jobs & might not have looked at the code for a while. | ||
- The code here is created as a personal side project with all the quality caveats that comes with it | ||
|
||
# Contributing | ||
|
||
There's several ways of contributing to this project, and effort has been made to make this as easy and transparent as possible, whether it's: | ||
|
||
- Reporting a bug | ||
- Discussing the current state of the code | ||
- Submitting a fix | ||
- Proposing new features | ||
- Becoming a maintainer | ||
|
||
## All code changes happen though pull requests (PRs) | ||
|
||
Pull requests are the best way to propose changes to the codebase (using the standard [Github Flow](https://guides.github.com/introduction/flow/index.html)). | ||
|
||
Some PR guidance: | ||
|
||
- Please keep PRs small and focused on a single feature or change, with discreet commits. Use multiple PRs if need be. | ||
- If you're thinking of adding a feature via a PR please create an issue first where it can be discussed. | ||
|
||
High level steps: | ||
|
||
1. Fork the repo and create your branch from `main`. | ||
2. If you've changed APIs, update the documentation. | ||
3. Ensure the test suite (if any) passes (run `make lint`). | ||
4. Make sure your code lints (run `make lint`). | ||
5. Issue that pull request! | ||
|
||
## Any contributions you make will be under the MIT Software License | ||
|
||
In short, when you submit code changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project. | ||
|
||
## Report bugs using Github's issues | ||
|
||
This project uses GitHub issues to track public bugs. Report a bug by [opening a new issue](./issues/new/choose) | ||
|
||
## Write bug reports with detail, background, and sample code | ||
|
||
**Great Bug Reports** tend to have: | ||
|
||
- A quick summary and/or background | ||
- Steps to reproduce | ||
- Be specific! | ||
- Give sample code if you can. Even if it's a snippet | ||
- What you expected would happen | ||
- What actually happens | ||
- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work) | ||
|
||
## Use a consistent coding style | ||
|
||
Run `make lint-fix` in order to format the code fix any formatting & linting issues that might be present. A [Prettier](https://prettier.io/) configuration file is included | ||
|
||
# References | ||
|
||
This document was heavily adapted from the open-source contribution guidelines found in [this gist](https://gist.github.com/briandk/3d2e8b3ec8daf5a27a62) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2022 Ben Coleman | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.EXPORT_ALL_VARIABLES: | ||
.PHONY: help lint lint-fix test | ||
.DEFAULT_GOAL := help | ||
|
||
help: ## 💬 This help message :) | ||
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' | ||
|
||
lint: ## 📝 Lint & format, check to be run in CI, sets exit code on error | ||
golangci-lint run --modules-download-mode=mod --timeout=4m ./... | ||
|
||
lint-fix: ## 🧙 Lint & format, fixes errors and modifies code | ||
golangci-lint run --modules-download-mode=mod --timeout=4m --fix ./... | ||
|
||
test: ## 🎯 Run integration tests | ||
@echo -e "WARNING: This will run integration tests\nThis will send several real emails!" | ||
go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package auth | ||
|
||
// HMAC-SHA256 signing for HTTP requests | ||
// Taken from https://learn.microsoft.com/en-us/azure/azure-app-configuration/rest-api-authentication-hmac#golang | ||
// Modified sightly to be flexible with the signature | ||
|
||
import ( | ||
"bytes" | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// SignRequestHMAC signs a HTTP request with HMAC-SHA256 | ||
func SignRequestHMAC(secret string, req *http.Request) error { | ||
method := req.Method | ||
host := req.URL.Host | ||
pathAndQuery := req.URL.Path | ||
|
||
if req.URL.RawQuery != "" { | ||
pathAndQuery = pathAndQuery + "?" + req.URL.RawQuery | ||
} | ||
|
||
content := []byte{} | ||
|
||
var err error | ||
|
||
if req.Body != nil { | ||
content, err = io.ReadAll(req.Body) | ||
if err != nil { | ||
// return err | ||
content = []byte{} | ||
} | ||
} | ||
|
||
req.Body = io.NopCloser(bytes.NewBuffer(content)) | ||
|
||
key, err := base64.StdEncoding.DecodeString(secret) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
timestamp := time.Now().UTC().Format(http.TimeFormat) | ||
contentHash := GetContentHashBase64(content) | ||
stringToSign := fmt.Sprintf("%s\n%s\n%s;%s;%s", strings.ToUpper(method), pathAndQuery, timestamp, host, contentHash) | ||
signature := GetHmac(stringToSign, key) | ||
|
||
req.Header.Set("x-ms-content-sha256", contentHash) | ||
req.Header.Set("x-ms-date", timestamp) | ||
|
||
req.Header.Set("Authorization", "HMAC-SHA256 SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature="+signature) | ||
|
||
return nil | ||
} | ||
|
||
// Hash content with SHA256 and return the hash in base64 | ||
func GetContentHashBase64(content []byte) string { | ||
hasher := sha256.New() | ||
hasher.Write(content) | ||
|
||
return base64.StdEncoding.EncodeToString(hasher.Sum(nil)) | ||
} | ||
|
||
// Hash content with HMAC-SHA256 and return the hash in base64 | ||
func GetHmac(content string, key []byte) string { | ||
hmac := hmac.New(sha256.New, key) | ||
hmac.Write([]byte(content)) | ||
|
||
return base64.StdEncoding.EncodeToString(hmac.Sum(nil)) | ||
} |
Oops, something went wrong.