|
| 1 | +/* |
| 2 | + Copyright NetFoundry Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package routes |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/go-openapi/runtime/middleware" |
| 24 | + "github.com/google/uuid" |
| 25 | + "github.com/openziti/edge-api/rest_management_api_server/operations/revocation" |
| 26 | + "github.com/openziti/edge-api/rest_model" |
| 27 | + "github.com/openziti/foundation/v2/errorz" |
| 28 | + "github.com/openziti/storage/boltz" |
| 29 | + "github.com/openziti/ziti/v2/controller/env" |
| 30 | + "github.com/openziti/ziti/v2/controller/model" |
| 31 | + "github.com/openziti/ziti/v2/controller/models" |
| 32 | + "github.com/openziti/ziti/v2/controller/permissions" |
| 33 | + "github.com/openziti/ziti/v2/controller/response" |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + r := NewRevocationRouter() |
| 38 | + env.AddRouter(r) |
| 39 | +} |
| 40 | + |
| 41 | +// RevocationRouter handles Management API routes for revocation management. |
| 42 | +type RevocationRouter struct { |
| 43 | + BasePath string |
| 44 | +} |
| 45 | + |
| 46 | +// NewRevocationRouter creates a new RevocationRouter. |
| 47 | +func NewRevocationRouter() *RevocationRouter { |
| 48 | + return &RevocationRouter{ |
| 49 | + BasePath: "/" + EntityNameRevocations, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (r *RevocationRouter) Register(ae *env.AppEnv) { |
| 54 | + ae.ManagementApi.RevocationListRevocationsHandler = revocation.ListRevocationsHandlerFunc(func(params revocation.ListRevocationsParams, _ interface{}) middleware.Responder { |
| 55 | + ae.InitPermissionsContext(params.HTTPRequest, permissions.Management, "revocation", permissions.Read) |
| 56 | + return ae.IsAllowed(r.List, params.HTTPRequest, "", "", permissions.DefaultManagementAccess()) |
| 57 | + }) |
| 58 | + |
| 59 | + ae.ManagementApi.RevocationDetailRevocationHandler = revocation.DetailRevocationHandlerFunc(func(params revocation.DetailRevocationParams, _ interface{}) middleware.Responder { |
| 60 | + ae.InitPermissionsContext(params.HTTPRequest, permissions.Management, "revocation", permissions.Read) |
| 61 | + return ae.IsAllowed(r.Detail, params.HTTPRequest, params.ID, "", permissions.DefaultManagementAccess()) |
| 62 | + }) |
| 63 | + |
| 64 | + ae.ManagementApi.RevocationCreateRevocationHandler = revocation.CreateRevocationHandlerFunc(func(params revocation.CreateRevocationParams, _ interface{}) middleware.Responder { |
| 65 | + ae.InitPermissionsContext(params.HTTPRequest, permissions.Management, "revocation", permissions.Create) |
| 66 | + return ae.IsAllowed(func(ae *env.AppEnv, rc *response.RequestContext) { r.Create(ae, rc, params) }, params.HTTPRequest, "", "", permissions.DefaultManagementAccess()) |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func (r *RevocationRouter) List(ae *env.AppEnv, rc *response.RequestContext) { |
| 71 | + ListWithHandler[*model.Revocation](ae, rc, ae.Managers.Revocation, MapRevocationToRestEntity) |
| 72 | +} |
| 73 | + |
| 74 | +func (r *RevocationRouter) Detail(ae *env.AppEnv, rc *response.RequestContext) { |
| 75 | + DetailWithHandler[*model.Revocation](ae, rc, ae.Managers.Revocation, MapRevocationToRestEntity) |
| 76 | +} |
| 77 | + |
| 78 | +// Create handles POST /revocations. It validates the submitted id against the |
| 79 | +// revocation type, computes the expiry from the configured refresh token duration, |
| 80 | +// and persists the revocation entry. |
| 81 | +func (r *RevocationRouter) Create(ae *env.AppEnv, rc *response.RequestContext, params revocation.CreateRevocationParams) { |
| 82 | + Create(rc, rc, RevocationLinkFactory, func() (string, error) { |
| 83 | + entity, err := mapCreateRevocationToModel(ae, rc.Request, params.Revocation) |
| 84 | + if err != nil { |
| 85 | + return "", err |
| 86 | + } |
| 87 | + if err = ae.Managers.Revocation.Create(entity, rc.NewChangeContext()); err != nil { |
| 88 | + return "", err |
| 89 | + } |
| 90 | + return entity.Id, nil |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +// mapCreateRevocationToModel converts a RevocationCreate REST model to a model.Revocation, |
| 95 | +// validating the id against the revocation type and computing the server-side expiry. |
| 96 | +func mapCreateRevocationToModel(ae *env.AppEnv, r *http.Request, create *rest_model.RevocationCreate) (*model.Revocation, error) { |
| 97 | + if create == nil { |
| 98 | + return nil, errorz.NewUnhandled(nil) |
| 99 | + } |
| 100 | + |
| 101 | + id := "" |
| 102 | + if create.ID != nil { |
| 103 | + id = *create.ID |
| 104 | + } |
| 105 | + |
| 106 | + revocationType := rest_model.RevocationTypeEnumJTI |
| 107 | + if create.Type != nil { |
| 108 | + revocationType = *create.Type |
| 109 | + } |
| 110 | + |
| 111 | + switch revocationType { |
| 112 | + case rest_model.RevocationTypeEnumIDENTITY: |
| 113 | + if _, err := ae.Managers.Identity.Read(id); err != nil { |
| 114 | + if boltz.IsErrNotFoundErr(err) { |
| 115 | + return nil, errorz.NewNotFound() |
| 116 | + } |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + case rest_model.RevocationTypeEnumJTI, rest_model.RevocationTypeEnumAPISESSION: |
| 120 | + if _, err := uuid.Parse(id); err != nil { |
| 121 | + return nil, errorz.NewFieldError("must be a valid UUID v4", "id", id) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + expiresAt := time.Now().Add(ae.GetConfig().Edge.Oidc.RefreshTokenDuration) |
| 126 | + |
| 127 | + return &model.Revocation{ |
| 128 | + BaseEntity: models.BaseEntity{ |
| 129 | + Id: id, |
| 130 | + Tags: TagsOrDefault(create.Tags), |
| 131 | + }, |
| 132 | + ExpiresAt: expiresAt, |
| 133 | + Type: string(revocationType), |
| 134 | + }, nil |
| 135 | +} |
0 commit comments