Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OIDC access token validation #107

Merged
merged 27 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func main() {
healthModule{}.Register(r.Group("/api"))

if config.HTTP.OIDC.Enable {
rsaKeys := GetOIDCPublicKeys(config.HTTP.OIDC)
rsaKeys, err := GetOIDCPublicKeys(config.HTTP.OIDC)
if err != nil {
panic(err)
}
r.Use(VerifyTokenMiddleware(config.HTTP.OIDC, rsaKeys))
SubscribeToKeyURLUpdates(config.HTTP.OIDC, rsaKeys)
}
Expand Down
34 changes: 24 additions & 10 deletions oidc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rsa"
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
"net/http"
"strings"
Expand Down Expand Up @@ -34,29 +35,38 @@ import (
// limitations under the License.

// GetOIDCPublicKeys return the public keys of the currently set WHARF_HTTP_OIDC_KeysURL.
fredx30 marked this conversation as resolved.
Show resolved Hide resolved
func GetOIDCPublicKeys(config OIDCConfig) *map[string]*rsa.PublicKey {
func GetOIDCPublicKeys(config OIDCConfig) (*map[string]*rsa.PublicKey, error) {
rsaKeys := make(map[string]*rsa.PublicKey)
var body map[string]interface{}
resp, err := http.Get(config.KeysURL)
if err != nil {
log.Error().WithError(err).Message("Could not fetch from KeysURL.")
return nil, fmt.Errorf("http GET keys URL: %w", err)
//log.Error().WithError(err).Message("Could not fetch from KeysURL.")
fredx30 marked this conversation as resolved.
Show resolved Hide resolved
}
var body struct {
Keys []struct {
KeyID string `json:"kid"`
Number string `json:"n"`
} `json:"keys"`
}
err = json.NewDecoder(resp.Body).Decode(&body)
if err != nil {
log.Error().WithError(err).Message("Failed to decode login JWT Keys.")
return nil, fmt.Errorf("decode keys payload: %w", err)
//log.Error().WithError(err).Message("Failed to decode login JWT Keys.")
fredx30 marked this conversation as resolved.
Show resolved Hide resolved
}
log.Debug().Message("Updating keys for oidc.")
rsaExponent := 65537
for _, bodykey := range body["keys"].([]interface{}) {
key := bodykey.(map[string]interface{})
kid := key["kid"].(string)
for _, key := range body.Keys {
kid := key.KeyID
rsakey := new(rsa.PublicKey)
number, _ := base64.RawURLEncoding.DecodeString(key["n"].(string))
number, err := base64.RawURLEncoding.DecodeString(key.Number)
if err != nil {
return nil, fmt.Errorf("decode JWT 'n' field: %w", err)
}
rsakey.N = new(big.Int).SetBytes(number)
rsakey.E = rsaExponent
rsaKeys[kid] = rsakey
}
return &rsaKeys
return &rsaKeys, nil
}

// VerifyTokenMiddleware is a gin middleware function that enforces validity of the access bearer token on every
Expand Down Expand Up @@ -105,7 +115,11 @@ func SubscribeToKeyURLUpdates(config OIDCConfig, rsakeys *map[string]*rsa.Public
go func() {
for {
<-fetchOidcKeysTicker.C
fredx30 marked this conversation as resolved.
Show resolved Hide resolved
rsakeys = GetOIDCPublicKeys(config)
newKeys, err := GetOIDCPublicKeys(config)
if err != nil {
panic("Cannot live with error: ")
}
*rsakeys = *newKeys
}
}()
}
18 changes: 18 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,21 @@ func TestNewLikeContainsValue(t *testing.T) {
})
}
}

func TestPointers(t *testing.T) {
myMap := map[string]int{
"foo": 123,
}

myMapPtr := &myMap

var f = func(localMapPtr *map[string]int) {
myNewMap := map[string]int{
"bar": 456,
}
localMapPtr = &myNewMap
applejag marked this conversation as resolved.
Show resolved Hide resolved
}

f(myMapPtr)
assert.Equal(t, 456, (*myMapPtr)["bar"])
}