PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
oauth2_middleware.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
19#pragma once
20
24
25#include <memory>
26#include <optional>
27#include <string>
28#include <string_view>
29#include <vector>
30
31// Forward declarations
32namespace crow {
33struct request;
34struct response;
35} // namespace crow
36
37namespace kcenon::pacs::security {
38class access_control_manager;
39} // namespace kcenon::pacs::security
40
41// user_context must be fully defined for std::optional<user_context>
43
45
46// =============================================================================
47// DICOMweb OAuth Scopes
48// =============================================================================
49
53namespace dicomweb_scopes {
54constexpr std::string_view read = "dicomweb.read";
55constexpr std::string_view search = "dicomweb.search";
56constexpr std::string_view write = "dicomweb.write";
57constexpr std::string_view delete_resource = "dicomweb.delete";
58} // namespace dicomweb_scopes
59
60// =============================================================================
61// Authentication Result
62// =============================================================================
63
75
76// =============================================================================
77// OAuth 2.0 Middleware
78// =============================================================================
79
112public:
117 explicit oauth2_middleware(const oauth2_config& config);
118
123 void set_jwks_provider(std::shared_ptr<jwks_provider> provider);
124
130 std::shared_ptr<security::access_control_manager> manager);
131
144 [[nodiscard]] std::optional<auth_result> authenticate(
145 const crow::request& req, crow::response& res) const;
146
158 [[nodiscard]] bool require_scope(
159 const jwt_claims& claims,
160 crow::response& res,
161 std::string_view required_scope) const;
162
171 [[nodiscard]] bool require_any_scope(
172 const jwt_claims& claims,
173 crow::response& res,
174 const std::vector<std::string>& required_scopes) const;
175
179 [[nodiscard]] bool enabled() const noexcept;
180
184 [[nodiscard]] const jwt_validator& validator() const noexcept;
185
186private:
189 std::shared_ptr<jwks_provider> jwks_provider_;
190 std::shared_ptr<security::access_control_manager> security_manager_;
191
193 [[nodiscard]] std::optional<std::string_view> extract_bearer_token(
194 const crow::request& req) const;
195
197 [[nodiscard]] bool verify_signature(const jwt_token& token) const;
198
200 static void set_unauthorized(crow::response& res,
201 std::string_view message);
202
204 static void set_forbidden(crow::response& res,
205 std::string_view message);
206};
207
208} // namespace kcenon::pacs::web::auth
Represents the security context for a user session.
bool enabled() const noexcept
Check if OAuth 2.0 is enabled.
std::optional< auth_result > authenticate(const crow::request &req, crow::response &res) const
Authenticate a request using OAuth 2.0 Bearer token.
std::shared_ptr< jwks_provider > jwks_provider_
bool require_any_scope(const jwt_claims &claims, crow::response &res, const std::vector< std::string > &required_scopes) const
Check if the request has any of the required scopes.
static void set_unauthorized(crow::response &res, std::string_view message)
Set 401 Unauthorized response.
bool require_scope(const jwt_claims &claims, crow::response &res, std::string_view required_scope) const
Check if the authenticated request has a required scope.
std::optional< std::string_view > extract_bearer_token(const crow::request &req) const
Extract Bearer token from Authorization header.
void set_jwks_provider(std::shared_ptr< jwks_provider > provider)
Set the JWKS provider for signature verification.
bool verify_signature(const jwt_token &token) const
Verify token signature using JWKS keys.
void set_access_control_manager(std::shared_ptr< security::access_control_manager > manager)
Set the access control manager for RBAC integration.
oauth2_middleware(const oauth2_config &config)
Construct middleware with OAuth 2.0 configuration.
static void set_forbidden(crow::response &res, std::string_view message)
Set 403 Forbidden response.
const jwt_validator & validator() const noexcept
Get the underlying JWT validator.
std::shared_ptr< security::access_control_manager > security_manager_
JSON Web Key Set (JWKS) provider with key caching.
JWT (JSON Web Token) validation for OAuth 2.0.
constexpr std::string_view delete_resource
OAuth 2.0 configuration for DICOMweb endpoints.
Result of a successful OAuth 2.0 authentication.
Decoded JWT claims (payload)
Decoded JWT token with raw segments for signature verification.
OAuth 2.0 configuration for DICOMweb authorization.
User context for session-based access control.