-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add JSON API for FDO Owner Server with OpenAPI specification #130
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
Open
djach7
wants to merge
1
commit into
fido-device-onboard:main
Choose a base branch
from
djach7:owner-api-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -6,3 +6,6 @@ go-fdo-server | |
| go-fdo-server-*.tar.* | ||
| rpmbuild | ||
| test/workdir | ||
|
|
||
| # Generated OpenAPI code | ||
| generated/ | ||
This file contains hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| // SPDX-FileCopyrightText: (C) 2024 Intel Corporation | ||
| // SPDX-License-Identifier: Apache 2.0 | ||
|
|
||
| package handlers | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "net/http" | ||
|
|
||
| "github.com/fido-device-onboard/go-fdo-server/internal/utils" | ||
| ) | ||
|
|
||
| // VoucherResponse represents a voucher in JSON format | ||
| type VoucherResponse struct { | ||
| Voucher string `json:"voucher"` | ||
| Encoding string `json:"encoding"` | ||
| GUID string `json:"guid"` | ||
| } | ||
|
|
||
| // VoucherInsertResponse represents the result of voucher insertion | ||
| type VoucherInsertResponse struct { | ||
| Processed int `json:"processed"` | ||
| Inserted int `json:"inserted"` | ||
| Errors []string `json:"errors,omitempty"` | ||
| } | ||
|
|
||
| // ErrorResponse represents a standard error response | ||
| type ErrorResponse struct { | ||
| Error string `json:"error"` | ||
| Details string `json:"details,omitempty"` | ||
| } | ||
|
|
||
| // WriteJSONVoucher writes a voucher response in JSON format | ||
| func WriteJSONVoucher(w http.ResponseWriter, voucherData []byte, encoding string, guid []byte) error { | ||
| response := VoucherResponse{ | ||
| Voucher: base64.StdEncoding.EncodeToString(voucherData), | ||
| Encoding: encoding, | ||
| GUID: hex.EncodeToString(guid), | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| return json.NewEncoder(w).Encode(response) | ||
| } | ||
|
|
||
| // WriteJSONError writes a standard error response in JSON format | ||
| func WriteJSONError(w http.ResponseWriter, statusCode int, errorMsg, details string) { | ||
| response := ErrorResponse{ | ||
| Error: errorMsg, | ||
| Details: details, | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(statusCode) | ||
| json.NewEncoder(w).Encode(response) | ||
| } | ||
|
|
||
| // WriteJSONVoucherInsertResponse writes a voucher insert response in JSON format | ||
| func WriteJSONVoucherInsertResponse(w http.ResponseWriter, response VoucherInsertResponse) error { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if len(response.Errors) > 0 && response.Inserted == 0 { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| } else { | ||
| w.WriteHeader(http.StatusOK) | ||
| } | ||
| return json.NewEncoder(w).Encode(response) | ||
| } | ||
|
|
||
| // ShouldReturnJSON checks if the client prefers JSON response | ||
| // This allows for backward compatibility with existing PEM clients | ||
| func ShouldReturnJSON(r *http.Request) bool { | ||
| accept := r.Header.Get("Accept") | ||
|
|
||
| // If client specifically requests PEM, honor it for backward compatibility | ||
| if accept == "application/x-pem-file" { | ||
| return false | ||
| } | ||
|
|
||
| // Default to JSON for everything else (including no preference) | ||
| return true | ||
| } | ||
|
|
||
| // WriteErrorResponse writes an error response in JSON or text format based on Accept header | ||
| func WriteErrorResponse(w http.ResponseWriter, r *http.Request, statusCode int, jsonMsg, jsonDetails, textMsg string) { | ||
| if ShouldReturnJSON(r) { | ||
| WriteJSONError(w, statusCode, jsonMsg, jsonDetails) | ||
| } else { | ||
| http.Error(w, textMsg, statusCode) | ||
| } | ||
| } | ||
|
|
||
| // ValidateAndDecodeGUID validates a GUID string and returns the decoded bytes | ||
| // Returns the decoded GUID bytes or an error response written to the writer | ||
| func ValidateAndDecodeGUID(w http.ResponseWriter, r *http.Request, guidHex string) ([]byte, bool) { | ||
| if !utils.IsValidGUID(guidHex) { | ||
| WriteErrorResponse(w, r, http.StatusBadRequest, "Invalid GUID", "GUID must be 32 hexadecimal characters", "Invalid GUID") | ||
| return nil, false | ||
| } | ||
|
|
||
| guid, err := hex.DecodeString(guidHex) | ||
| if err != nil { | ||
| WriteErrorResponse(w, r, http.StatusBadRequest, "Invalid GUID format", err.Error(), "Invalid GUID format") | ||
| return nil, false | ||
| } | ||
|
|
||
| return guid, true | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using a Makefile target to generate the api I would prefer to use the golang native approach to generate code, please see https://github.com/oapi-codegen/oapi-codegen/?tab=readme-ov-file#for-go-124 as an example. We will need a Makefile target anyway but in this case it will run
go generateand we will make thebuildtarget to depend on it.This leads me to open the discussion about using the openapi generator tools (java) or the more native
oapi-codegenshown in the link above.After thinking a lot about this I think we should use the native tool as it's very well documented and used by other projects, but as I said this is open to discussion.