feat(auth): Implement GitHub OAuth authentication flow for package publishing
- Added authentication service and GitHub OAuth integration. - Introduced new authentication methods and structures in the model. - Updated the API to handle authentication during the publishing process. - Created handlers for starting and checking the status of the OAuth flow. - Enhanced the publish handler to validate authentication credentials. - Updated configuration to include GitHub OAuth settings. - Added tests for the OAuth flow and publishing with authentication. - Updated documentation to reflect changes in the API and authentication process.
This commit is contained in:
parent
582d93a103
commit
2089c5aaa1
2 changed files with 924 additions and 35 deletions
117
internal/api/handlers/v0/auth.go
Normal file
117
internal/api/handlers/v0/auth.go
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
// Package v0 contains API handlers for version 0 of the API
|
||||
package v0
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/auth"
|
||||
"github.com/modelcontextprotocol/registry/internal/model"
|
||||
)
|
||||
|
||||
// StartAuthHandler handles requests to start an authentication flow
|
||||
func StartAuthHandler(authService auth.Service) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Only allow POST method
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Read the request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "Error reading request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
// Parse request body into AuthRequest struct
|
||||
var authReq struct {
|
||||
Method string `json:"method"`
|
||||
RepoRef string `json:"repo_ref"`
|
||||
}
|
||||
err = json.Unmarshal(body, &authReq)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request payload: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if authReq.Method == "" {
|
||||
http.Error(w, "Auth method is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Convert string method to enum type
|
||||
var method model.AuthMethod
|
||||
switch authReq.Method {
|
||||
case "github":
|
||||
method = model.AuthMethodGitHub
|
||||
default:
|
||||
http.Error(w, "Unsupported authentication method", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Start auth flow
|
||||
flowInfo, statusToken, err := authService.StartAuthFlow(r.Context(), method, authReq.RepoRef)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to start auth flow: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Return successful response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"flow_info": flowInfo,
|
||||
"status_token": statusToken,
|
||||
"expires_in": 300, // 5 minutes
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// CheckAuthStatusHandler handles requests to check the status of an authentication flow
|
||||
func CheckAuthStatusHandler(authService auth.Service) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Only allow GET method
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Get status token from query parameter
|
||||
statusToken := r.URL.Query().Get("token")
|
||||
if statusToken == "" {
|
||||
http.Error(w, "Status token is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Check auth status
|
||||
token, err := authService.CheckAuthStatus(r.Context(), statusToken)
|
||||
if err != nil {
|
||||
if err.Error() == "pending" {
|
||||
// Auth is still pending
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"status": "pending",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Other error
|
||||
http.Error(w, "Failed to check auth status: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Authentication completed successfully
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"status": "complete",
|
||||
"token": token,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -6,12 +6,24 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/auth"
|
||||
"github.com/modelcontextprotocol/registry/internal/model"
|
||||
"github.com/modelcontextprotocol/registry/internal/service"
|
||||
)
|
||||
|
||||
// httpError represents an HTTP error with a message and status code
|
||||
type httpError struct {
|
||||
msg string
|
||||
status int
|
||||
}
|
||||
|
||||
// Error returns the error message
|
||||
func (e *httpError) Error() string {
|
||||
return e.msg
|
||||
}
|
||||
|
||||
// PublishHandler handles requests to publish new server details to the registry
|
||||
func PublishHandler(registry service.RegistryService) http.HandlerFunc {
|
||||
func PublishHandler(registry service.RegistryService, authService auth.Service) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Only allow POST method
|
||||
if r.Method != http.MethodPost {
|
||||
|
|
@ -27,26 +39,48 @@ func PublishHandler(registry service.RegistryService) http.HandlerFunc {
|
|||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
// Parse request body into ServerDetail struct
|
||||
var serverDetail model.ServerDetail
|
||||
err = json.Unmarshal(body, &serverDetail)
|
||||
// Parse request body into PublishRequest struct
|
||||
var publishReq model.PublishRequest
|
||||
err = json.Unmarshal(body, &publishReq)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
http.Error(w, "Invalid request payload: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get server details from the request
|
||||
serverDetail := publishReq.ServerDetail
|
||||
|
||||
// Validate required fields
|
||||
if serverDetail.Name == "" {
|
||||
http.Error(w, "Name is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// version is required
|
||||
// Version is required
|
||||
if serverDetail.VersionDetail.Version == "" {
|
||||
http.Error(w, "Version is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate authentication credentials
|
||||
if authService != nil {
|
||||
publishReq.Authentication.RepoRef = serverDetail.Name
|
||||
valid, err := authService.ValidateAuth(r.Context(), publishReq.Authentication)
|
||||
if err != nil {
|
||||
if err == auth.ErrAuthRequired {
|
||||
http.Error(w, "Authentication is required for publishing", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Authentication failed: "+err.Error(), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
if !valid {
|
||||
http.Error(w, "Invalid authentication credentials", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Call the publish method on the registry service
|
||||
err = registry.Publish(&serverDetail)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Reference in a new issue