Skip to content

Commit

Permalink
Working on documentation, TBD versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
igorrendulic committed Aug 5, 2020
1 parent 32ee757 commit 3b92e49
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 59 deletions.
396 changes: 372 additions & 24 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backpressure/backpressure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestBackpressure(t *testing.T) {
}
defer bckPress.Close()

numEventsToSend := 15000 // 30K events
numEventsToSend := 15000 // 15K events

numEventsSent := 0

Expand Down
29 changes: 29 additions & 0 deletions crypto/hmac.go
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)

}
16 changes: 16 additions & 0 deletions crypto/hmac_test.go
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")
}
}
25 changes: 0 additions & 25 deletions crypto/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
package crypto

import (
"crypto/hmac"
"crypto/rand"
"encoding/hex"
"hash"

"golang.org/x/crypto/bcrypt"
)
Expand Down Expand Up @@ -48,26 +46,3 @@ func GenerateSecretKey(numBytes int) (string, error) {

return string(dst), nil
}

func ComputeHmac256(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))
}

// ValidateHMac256Signature validates signature from a sent payload
// Bash example creating signature: apisig=`echo -n "$nonce$key" | openssl dgst -sha256 -hmac "mysecret" -binary | xxd -p -c 256`
func ValidateHmac256Signature(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)

}
16 changes: 7 additions & 9 deletions crypto/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package crypto

import (
"crypto/sha256"
"fmt"
"testing"
)

Expand All @@ -31,11 +29,11 @@ func TestPasswordHash(t *testing.T) {
}
}

func TestHMac256(t *testing.T) {
payload := "this is example payload"
mac := ComputeHmac256(sha256.New, payload, "mysecret")
fmt.Printf("hmac: %v\n", mac)

isValid := ValidateHmac256Signature(sha256.New, payload, "mysecret", mac)
fmt.Printf("is valid: %v\n", isValid)
func BenchmarkBcrypt(b *testing.B) {
b.StopTimer()
pass, _ := HashPassword("this is test")
b.StartTimer()
for i := 0; i < b.N; i++ {
CheckPasswordHash(pass, "this is test")
}
}
1 change: 1 addition & 0 deletions example/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ auth_token:
enabled: false
header: "authkey"
token: "abc"
path: /api/v1/micro/*

jwt_token:
enabled: false
Expand Down
14 changes: 14 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// 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 main

import (
Expand Down
25 changes: 25 additions & 0 deletions models/jwt/user_claim.go
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
}

0 comments on commit 3b92e49

Please sign in to comment.