-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "[TT-2539] added access/transaction logs" (#6524)"
This reverts commit 3e435cc.
- Loading branch information
Showing
15 changed files
with
507 additions
and
128 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 |
---|---|---|
|
@@ -50,6 +50,7 @@ session_state_gen_test.go | |
__pycache__/ | ||
tyk.test | ||
tyk-gateway.pid | ||
*.go-e | ||
|
||
tyk_linux_* | ||
.aider* | ||
|
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
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,53 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"hash" | ||
|
||
"github.com/TykTechnologies/murmur3" | ||
) | ||
|
||
const ( | ||
HashSha256 = "sha256" | ||
HashMurmur32 = "murmur32" | ||
HashMurmur64 = "murmur64" | ||
HashMurmur128 = "murmur128" | ||
) | ||
|
||
func hashFunction(algorithm string) (hash.Hash, error) { | ||
switch algorithm { | ||
case HashSha256: | ||
return sha256.New(), nil | ||
case HashMurmur64: | ||
return murmur3.New64(), nil | ||
case HashMurmur128: | ||
return murmur3.New128(), nil | ||
case "", HashMurmur32: | ||
return murmur3.New32(), nil | ||
default: | ||
return murmur3.New32(), fmt.Errorf("Unknown key hash function: %s. Falling back to murmur32.", algorithm) | ||
} | ||
} | ||
|
||
func HashStr(in string, withAlg ...string) string { | ||
var algo string | ||
if len(withAlg) > 0 && withAlg[0] != "" { | ||
algo = withAlg[0] | ||
} else { | ||
algo = TokenHashAlgo(in) | ||
} | ||
|
||
h, _ := hashFunction(algo) | ||
h.Write([]byte(in)) | ||
return hex.EncodeToString(h.Sum(nil)) | ||
} | ||
|
||
func HashKey(in string, hashKey bool) string { | ||
if !hashKey { | ||
// Not hashing? Return the raw key | ||
return in | ||
} | ||
return HashStr(in) | ||
} |
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,83 @@ | ||
package crypto | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/buger/jsonparser" | ||
|
||
"github.com/TykTechnologies/tyk/internal/uuid" | ||
) | ||
|
||
// `{"` in base64 | ||
const B64JSONPrefix = "ey" | ||
|
||
const DefaultHashAlgorithm = "murmur64" | ||
|
||
const MongoBsonIdLength = 24 | ||
|
||
// GenerateToken generates a token. | ||
// If hashing algorithm is empty, it uses legacy key generation. | ||
func GenerateToken(orgID, keyID, hashAlgorithm string) (string, error) { | ||
if keyID == "" { | ||
keyID = uuid.NewHex() | ||
} | ||
|
||
if hashAlgorithm != "" { | ||
_, err := hashFunction(hashAlgorithm) | ||
if err != nil { | ||
hashAlgorithm = DefaultHashAlgorithm | ||
} | ||
|
||
jsonToken := fmt.Sprintf(`{"org":"%s","id":"%s","h":"%s"}`, orgID, keyID, hashAlgorithm) | ||
Check failure Code scanning / CodeQL Potentially unsafe quoting Critical
If this
JSON value Error loading related location Loading If this JSON value Error loading related location Loading If this JSON value Error loading related location Loading |
||
return base64.StdEncoding.EncodeToString([]byte(jsonToken)), err | ||
} | ||
|
||
// Legacy keys | ||
return orgID + keyID, nil | ||
} | ||
|
||
func TokenHashAlgo(token string) string { | ||
// Legacy tokens not b64 and not JSON records | ||
if strings.HasPrefix(token, B64JSONPrefix) { | ||
if jsonToken, err := base64.StdEncoding.DecodeString(token); err == nil { | ||
hashAlgo, _ := jsonparser.GetString(jsonToken, "h") | ||
return hashAlgo | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func TokenID(token string) (id string, err error) { | ||
jsonToken, err := base64.StdEncoding.DecodeString(token) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return jsonparser.GetString(jsonToken, "id") | ||
} | ||
|
||
func TokenOrg(token string) string { | ||
if strings.HasPrefix(token, B64JSONPrefix) { | ||
if jsonToken, err := base64.StdEncoding.DecodeString(token); err == nil { | ||
// Checking error in case if it is a legacy tooken which just by accided has the same b64JSON prefix | ||
if org, err := jsonparser.GetString(jsonToken, "org"); err == nil { | ||
return org | ||
} | ||
} | ||
} | ||
|
||
// 24 is mongo bson id length | ||
if len(token) > MongoBsonIdLength { | ||
newToken := token[:MongoBsonIdLength] | ||
_, err := hex.DecodeString(newToken) | ||
if err == nil { | ||
return newToken | ||
} | ||
} | ||
|
||
return "" | ||
} |
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
Oops, something went wrong.