-
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.
Working on documentation, TBD versioning
- Loading branch information
1 parent
32ee757
commit 3b92e49
Showing
9 changed files
with
465 additions
and
59 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
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 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/hmac" | ||
"encoding/hex" | ||
"hash" | ||
) | ||
|
||
func ComputeHmac(hashFunc func() hash.Hash, message string, secret string) string { | ||
key := []byte(secret) | ||
h := hmac.New(hashFunc, key) | ||
h.Write([]byte(message)) | ||
return hex.EncodeToString(h.Sum(nil)) | ||
} | ||
|
||
// ValidateHMacSignature validates signature from a sent payload | ||
func ValidateHmacSignature(hashFunc func() hash.Hash, payload string, secret string, hexDigest string) bool { | ||
hHash := hmac.New(hashFunc, []byte(secret)) | ||
_, _ = hHash.Write([]byte(payload)) // assignations are required not to get an errcheck issue (linter) | ||
computedDigest := hHash.Sum(nil) | ||
|
||
digest, err := hex.DecodeString(hexDigest) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return hmac.Equal(computedDigest, digest) | ||
|
||
} |
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 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/sha256" | ||
"testing" | ||
) | ||
|
||
func TestHMac(t *testing.T) { | ||
payload := "this is example payload" | ||
mac := ComputeHmac(sha256.New, payload, "mysecret") | ||
|
||
isValid := ValidateHmacSignature(sha256.New, payload, "mysecret", mac) | ||
if !isValid { | ||
t.Error("hmac validation failed") | ||
} | ||
} |
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
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
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
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
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,25 @@ | ||
// Copyright 2020 Wearless Tech Inc All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package jwt | ||
|
||
import "github.com/dgrijalva/jwt-go" | ||
|
||
// UserClaim for JWT user within the context | ||
type UserClaim struct { | ||
ID string `json:"id"` | ||
Roles []string `json:"Roles,omitempty"` | ||
Enabled bool `json:"enabled"` | ||
jwt.StandardClaims | ||
} |