/
oidc_middleware.go
161 lines (151 loc) · 5.22 KB
/
oidc_middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"crypto/rsa"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/iver-wharf/wharf-core/pkg/ginutil"
"github.com/iver-wharf/wharf-core/pkg/problem"
"github.com/golang-jwt/jwt/v4"
)
// This is a modified version of the code provided in the follow blog post and
// GitHub repository:
// - https://developer.okta.com/blog/2021/01/04/offline-jwt-validation-with-go
// - https://github.com/oktadev/okta-offline-jwt-validation-example/tree/a61cc73bf893686c1efe67ce86448047205826bc
//
// Copyright 2019 Okta, Inc.
//
// 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.
// GetOIDCPublicKeys returns the public keys of the currently set WHARF_HTTP_OIDC_KEYSURL.
func GetOIDCPublicKeys(keysURL string) (map[string]*rsa.PublicKey, error) {
rsaKeys := make(map[string]*rsa.PublicKey)
resp, err := http.Get(keysURL)
if err != nil {
return nil, fmt.Errorf("http GET keys URL: %w", err)
}
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 {
return nil, fmt.Errorf("decode keys payload: %w", err)
}
log.Debug().Message("Updating keys for oidc.")
rsaExponent := 65537
for _, key := range body.Keys {
kid := key.KeyID
rsakey := new(rsa.PublicKey)
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, nil
}
func newOIDCMiddleware(rsaKeys map[string]*rsa.PublicKey, config OIDCConfig) *oidcMiddleware {
return &oidcMiddleware{
rsaKeys: rsaKeys,
config: config,
}
}
type oidcMiddleware struct {
rsaKeys map[string]*rsa.PublicKey
config OIDCConfig
}
// VerifyTokenMiddleware is a gin middleware function that enforces validity of the access bearer token on every
// request. This uses the environment vars WHARF_HTTP_OIDC_ISSUERURL and WHARF_HTTP_OIDC_AUDIENCEURL as limiters
// that control the variety of tokens that pass validation.
func (m *oidcMiddleware) VerifyTokenMiddleware(ginContext *gin.Context) {
if m.rsaKeys == nil {
ginutil.WriteProblem(ginContext, problem.Response{
Type: "/prob/api/oidc/missing-rsa-keys",
Title: "Missing OIDC public keys.",
Status: http.StatusInternalServerError,
Detail: "The OIDC RSA public keys were not properly set up during initialization of the wharf-api.",
})
ginContext.Abort()
return
}
isValid := false
errorMessage := ""
tokenString := ginContext.Request.Header.Get("Authorization")
if !strings.HasPrefix(tokenString, "Bearer ") {
ginutil.WriteUnauthorized(ginContext, "Expected authorization scheme to be 'Bearer' (case sensitive), but was not.")
ginContext.Abort()
return
}
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if kid, ok := token.Header["kid"].(string); ok {
return m.rsaKeys[kid], nil
}
return nil, errors.New("expected JWT to have string 'kid' field")
})
if err != nil {
errorMessage = err.Error()
} else if !token.Valid {
errorMessage = "invalid access bearer token."
} else if token.Header["alg"] == nil {
errorMessage = "missing 'alg' field."
} else if token.Claims.(jwt.MapClaims)["aud"] != m.config.AudienceURL {
errorMessage = "invalid 'aud' field."
} else if iss, ok := token.Claims.(jwt.MapClaims)["iss"].(string); !ok {
errorMessage = "invalid or missing 'iss' field: should be string."
} else if !strings.Contains(iss, m.config.IssuerURL) {
errorMessage = "invalid 'iss' field: disallowed issuer."
} else {
isValid = true
}
if !isValid {
ginutil.WriteUnauthorized(ginContext, "Invalid JWT: "+errorMessage)
ginContext.Abort()
}
}
// SubscribeToKeyURLUpdates ensures new keys are fetched as necessary.
// As a standard OIDC login provider keys should be checked for updates ever 1 day 1 hour.
func (m *oidcMiddleware) SubscribeToKeyURLUpdates() {
fetchOidcKeysTicker := time.NewTicker(m.config.UpdateInterval)
log.Debug().WithDuration("interval", m.config.UpdateInterval).
Message("Subscribing to OIDC public keys rotation via periodic check timer.")
go func() {
for {
<-fetchOidcKeysTicker.C
m.updateOIDCPublicKeys()
}
}()
}
func (m *oidcMiddleware) updateOIDCPublicKeys() {
newKeys, err := GetOIDCPublicKeys(m.config.KeysURL)
if err != nil {
log.Warn().WithError(err).
WithDuration("interval", m.config.UpdateInterval).
Message("Failed to update OIDC public keys.")
} else {
m.rsaKeys = newKeys
log.Info().
WithDuration("interval", m.config.UpdateInterval).
Message("Successfully updated OIDC public keys.")
}
}