This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache tokens obtained from external commands
Signed-off-by: Fabio Grätz <fabiogratz@googlemail.com>
- Loading branch information
Fabio Grätz
committed
Sep 16, 2023
1 parent
35b7327
commit 3b7389f
Showing
8 changed files
with
146 additions
and
39 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
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,37 @@ | ||
package externalprocess | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type jwtClaims struct { | ||
Exp int64 `json:"exp"` | ||
} | ||
|
||
// When we receive a token from an external process, we don't have a key to validate it | ||
// For caching purposes, we still want to know when the token *allegedly* expires. | ||
func GetUnvalidatedTokenExpiration(tokenString string) (time.Time, error) { | ||
parts := strings.Split(tokenString, ".") | ||
if len(parts) != 3 { | ||
return time.Time{}, errors.New("invalid token") | ||
} | ||
|
||
payload, err := base64.RawURLEncoding.DecodeString(parts[1]) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
var claims jwtClaims | ||
err = json.Unmarshal(payload, &claims) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
expiry := time.Unix(claims.Exp, 0) | ||
|
||
return expiry, nil | ||
} |
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,24 @@ | ||
package externalprocess | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetUnvalidatedTokenExpiration(t *testing.T) { | ||
|
||
exp := time.Now().Unix() | ||
claims := jwtClaims{Exp: exp} | ||
payload, _ := json.Marshal(claims) | ||
token := "header." + base64.RawURLEncoding.EncodeToString(payload) + ".signature" | ||
|
||
expiry, err := GetUnvalidatedTokenExpiration(token) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
assert.Equal(t, exp, expiry.Unix()) | ||
} |
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