-
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.
Merge pull request #10 from OpenCHAMI/lritzdorf/rearchitect
Rearchitect to remove Gin server package, add JWT-protected HTTP routes
- Loading branch information
Showing
13 changed files
with
393 additions
and
233 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
dist | ||
dist | ||
# Our compiled binary | ||
/cloud-init-server |
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,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
Oops, something went wrong.