2021-07-23 15:41:24 +02:00
package api
import (
2021-12-09 09:36:03 +01:00
"fmt"
2021-07-23 15:41:24 +02:00
"net/http"
2022-03-24 18:34:04 +01:00
"go.step.sm/linkedca"
2022-03-30 11:22:22 +03:00
"github.com/smallstep/certificates/api/render"
2021-07-23 15:41:24 +02:00
"github.com/smallstep/certificates/authority/admin"
2022-01-07 16:59:55 +01:00
)
2021-07-23 15:41:24 +02:00
// CreateExternalAccountKeyRequest is the type for POST /admin/acme/eab requests
type CreateExternalAccountKeyRequest struct {
2021-10-08 14:29:44 +02:00
Reference string ` json:"reference" `
2021-07-23 15:41:24 +02:00
}
2021-09-16 23:09:24 +02:00
// Validate validates a new ACME EAB Key request body.
2021-08-27 12:39:37 +02:00
func ( r * CreateExternalAccountKeyRequest ) Validate ( ) error {
2021-12-09 09:36:03 +01:00
if len ( r . Reference ) > 256 { // an arbitrary, but sensible (IMO), limit
return fmt . Errorf ( "reference length %d exceeds the maximum (256)" , len ( r . Reference ) )
}
2021-08-27 12:39:37 +02:00
return nil
2021-07-23 15:41:24 +02:00
}
// GetExternalAccountKeysResponse is the type for GET /admin/acme/eab responses
type GetExternalAccountKeysResponse struct {
2022-01-24 14:03:56 +01:00
EAKs [ ] * linkedca . EABKey ` json:"eaks" `
NextCursor string ` json:"nextCursor" `
2021-07-23 15:41:24 +02:00
}
2021-10-08 14:29:44 +02:00
// requireEABEnabled is a middleware that ensures ACME EAB is enabled
// before serving requests that act on ACME EAB credentials.
2022-03-30 14:21:39 +02:00
func ( h * Handler ) requireEABEnabled ( next http . HandlerFunc ) http . HandlerFunc {
2021-10-08 14:29:44 +02:00
return func ( w http . ResponseWriter , r * http . Request ) {
2022-01-07 16:59:55 +01:00
ctx := r . Context ( )
2022-03-30 18:21:25 +02:00
prov := linkedca . ProvisionerFromContext ( ctx )
details := prov . GetDetails ( )
if details == nil {
render . Error ( w , admin . NewErrorISE ( "error getting details for provisioner '%s'" , prov . GetName ( ) ) )
2021-10-08 14:29:44 +02:00
return
}
2022-03-30 18:21:25 +02:00
acmeProvisioner := details . GetACME ( )
if acmeProvisioner == nil {
render . Error ( w , admin . NewErrorISE ( "error getting ACME details for provisioner '%s'" , prov . GetName ( ) ) )
2021-10-08 14:29:44 +02:00
return
}
2021-09-16 23:09:24 +02:00
2022-03-30 18:21:25 +02:00
if ! acmeProvisioner . RequireEab {
render . Error ( w , admin . NewError ( admin . ErrorBadRequestType , "ACME EAB not enabled for provisioner '%s'" , prov . GetName ( ) ) )
return
}
2021-09-16 23:09:24 +02:00
2022-03-31 16:12:29 +02:00
next ( w , r )
2021-09-16 23:09:24 +02:00
}
2022-01-07 16:59:55 +01:00
}
2022-02-08 13:26:30 +01:00
type acmeAdminResponderInterface interface {
GetExternalAccountKeys ( w http . ResponseWriter , r * http . Request )
CreateExternalAccountKey ( w http . ResponseWriter , r * http . Request )
DeleteExternalAccountKey ( w http . ResponseWriter , r * http . Request )
2021-09-16 23:09:24 +02:00
}
2022-02-08 13:26:30 +01:00
// ACMEAdminResponder is responsible for writing ACME admin responses
type ACMEAdminResponder struct { }
2021-07-23 15:41:24 +02:00
2022-02-08 13:26:30 +01:00
// NewACMEAdminResponder returns a new ACMEAdminResponder
func NewACMEAdminResponder ( ) * ACMEAdminResponder {
return & ACMEAdminResponder { }
2021-07-23 15:41:24 +02:00
}
2022-02-08 13:26:30 +01:00
// GetExternalAccountKeys writes the response for the EAB keys GET endpoint
func ( h * ACMEAdminResponder ) GetExternalAccountKeys ( w http . ResponseWriter , r * http . Request ) {
2022-03-30 11:22:22 +03:00
render . Error ( w , admin . NewError ( admin . ErrorNotImplementedType , "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm" ) )
2021-08-27 14:10:00 +02:00
}
2022-02-08 13:26:30 +01:00
// CreateExternalAccountKey writes the response for the EAB key POST endpoint
func ( h * ACMEAdminResponder ) CreateExternalAccountKey ( w http . ResponseWriter , r * http . Request ) {
2022-03-30 11:22:22 +03:00
render . Error ( w , admin . NewError ( admin . ErrorNotImplementedType , "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm" ) )
2022-02-08 13:26:30 +01:00
}
2021-08-27 16:58:04 +02:00
2022-02-08 13:26:30 +01:00
// DeleteExternalAccountKey writes the response for the EAB key DELETE endpoint
func ( h * ACMEAdminResponder ) DeleteExternalAccountKey ( w http . ResponseWriter , r * http . Request ) {
2022-03-30 11:22:22 +03:00
render . Error ( w , admin . NewError ( admin . ErrorNotImplementedType , "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm" ) )
2021-07-23 15:41:24 +02:00
}