This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
145 additions
and
19 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
- [ ] Add Context | ||
- [ ] Add Authentication | ||
- [ ] Add Abort Option | ||
- [ ] Add Abort Option | ||
- [ ] |
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 |
---|---|---|
@@ -1,16 +1,12 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= | ||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= | ||
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= | ||
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= | ||
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= | ||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM= | ||
golang.org/x/tools v0.9.3/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,119 @@ | ||
package middleware | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/golang-jwt/jwt" | ||
) | ||
|
||
type PublicKey struct { | ||
KTY string `json:"kty"` | ||
KID string `json:"kid"` | ||
Use string `json:"use"` | ||
N string `json:"n"` | ||
E string `json:"e"` | ||
X5C string `json:"x5c"` | ||
Issuer string `json:"issuer"` | ||
} | ||
|
||
func GetMsPublicKey() []PublicKey { | ||
microsoftKeysURL := "https://login.microsoftonline.com/common/discovery/v2.0/keys" | ||
|
||
client := &http.Client{} | ||
|
||
body := &bytes.Buffer{} | ||
req, err := http.NewRequest(http.MethodGet, microsoftKeysURL, body) | ||
if err != nil { | ||
log.Fatalf("unable to generate http req - %v", err) | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Fatalf("error executing http req - %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
log.Fatal("Status Not OK") | ||
} | ||
|
||
var publicKey []PublicKey | ||
err = resp.Body.Close() | ||
if err != nil { | ||
log.Fatalf("error closing resp body - %v", err) | ||
} | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&publicKey) | ||
if err != nil { | ||
log.Fatalf("error decoding resp body - %v", err) | ||
} | ||
|
||
return publicKey | ||
} | ||
|
||
type EnsureAuth struct { | ||
logHandler http.Handler | ||
} | ||
|
||
func (ea *EnsureAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
fmt.Println("hello - I am middleware :)") | ||
|
||
// get the authorization header | ||
authHeader := r.Header.Get("Authorization") | ||
if authHeader == "" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Write([]byte("missing authorization header")) | ||
return | ||
} | ||
|
||
// get the token | ||
token := authHeader[len("Bearer "):] | ||
|
||
// get the public key set | ||
publicKeySet := GetMsPublicKey() | ||
|
||
// verify the token | ||
// Parse the token without verifying the signature | ||
t, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) { | ||
// Don't forget to validate the alg is what you expect: | ||
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok { | ||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) | ||
} | ||
|
||
// find the public key | ||
var key string | ||
for _, v := range publicKeySet { | ||
if v.KID == token.Header["kid"] { | ||
key = v.X5C | ||
break | ||
} | ||
} | ||
|
||
// embed the public key in the PEM format | ||
pem := "-----BEGIN CERTIFICATE-----\n" + key + "\n-----END CERTIFICATE-----" | ||
|
||
// parse the PEM encoded public key | ||
result, err := jwt.ParseRSAPublicKeyFromPEM([]byte(pem)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
}) | ||
if err != nil { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
// print the claims | ||
fmt.Println(t.Claims) | ||
|
||
ea.logHandler.ServeHTTP(w, r) | ||
} | ||
|
||
func NewEnsureAuth(handlerToWrap http.Handler) *EnsureAuth { | ||
return &EnsureAuth{handlerToWrap} | ||
} |
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