-
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.
Implement JWT-based authentication for secure route
This copies much JWKS helper code (`cmd/cloud-init-server/auth.go`) from https://github.com/OpenCHAMI/smd/blob/cdac4f1a606401a2b150513b935143238e3eb026/cmd/smd/routers.go with certain irrelevant-to-us parts stripped out.
- Loading branch information
Showing
5 changed files
with
121 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
// Adapted from OpenCHAMI SMD's auth.go | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
jwtauth "github.com/OpenCHAMI/jwtauth/v5" | ||
"github.com/lestrrat-go/jwx/v2/jwk" | ||
) | ||
|
||
type statusCheckTransport struct { | ||
http.RoundTripper | ||
} | ||
|
||
func (ct *statusCheckTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
resp, err := http.DefaultTransport.RoundTrip(req) | ||
if err == nil && resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("status code: %d", resp.StatusCode) | ||
} | ||
|
||
return resp, err | ||
} | ||
|
||
func newHTTPClient() *http.Client { | ||
return &http.Client{Transport: &statusCheckTransport{}} | ||
} | ||
|
||
func fetchPublicKeyFromURL(url string) (*jwtauth.JWTAuth, error) { | ||
client := newHTTPClient() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
set, err := jwk.Fetch(ctx, url, jwk.WithHTTPClient(client)) | ||
if err != nil { | ||
msg := "%w" | ||
|
||
// if the error tree contains an EOF, it means that the response was empty, | ||
// so add a more descriptive message to the error tree | ||
if errors.Is(err, io.EOF) { | ||
msg = "received empty response for key: %w" | ||
} | ||
|
||
return nil, fmt.Errorf(msg, err) | ||
} | ||
jwks, err := json.Marshal(set) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal JWKS: %v", err) | ||
} | ||
keyset, err := jwtauth.NewKeySet(jwks) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to initialize JWKS: %v", err) | ||
} | ||
|
||
return keyset, 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
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