MantisBase v0.4.9
Loading...
Searching...
No Matches
mb::MbAuth Class Reference

JWT token creation and verification utilities. More...

#include <auth.h>

Public Member Functions

 MbAuth (const MantisBase &app)
MbOAuthMgr & oauth () const
MbApiKeyMgr & apiKey () const
std::string createToken (const json &claims_params, int timeout=-1) const
 Create JWT token with custom claims.
int sessionTimeoutSeconds (const std::string &entity_name, int timeout=-1) const
json verifyToken (const std::string &token) const
 Verify a JWT and return claims plus verification metadata.
bool deleteSession (const std::string &session_id) const
json refreshSession (const std::string &old_session_id, const std::string &entity_name, const std::string &user_id)
 Rotate a session: invalidate old_session_id and issue a new JWT.
json requestLoginOtp (const std::string &entity_name, const std::string &email) const
 Email a one-time sign-in code for an auth-entity account.
json loginWithOtp (const std::string &entity_name, const std::string &email, const std::string &otp) const
 Exchange a one-time sign-in code for a session token.
void rejectUnverifiedLogin (const std::string &entity_name, const json &user) const
 Reject login when the entity requires a verified email and the account is not verified.
void notifyEmailVerification (const std::string &entity_name, const std::string &user_id) const
 Email a verification link when SMTP is configured and the account is unverified. Missing SMTP or siteDomain is logged and ignored so registration can still succeed.
json requestEmailVerification (const std::string &entity_name, const std::string &email, const std::string &request_host) const
 Send a verification link for an email address. Unknown addresses return { "sent": true } and send nothing.
json confirmEmailVerification (const std::string &entity_name, const std::string &token) const
 Mark the account verified when token matches a live verification link.
json requestPasswordReset (const std::string &entity_name, const std::string &email, const std::string &request_host) const
 Email a password-reset link for an account. Unknown addresses return { "sent": true } and send nothing.
json confirmPasswordReset (const std::string &entity_name, const std::string &token, const std::string &new_password) const
 Set a new password when token matches a live reset link.

Detailed Description

JWT token creation and verification utilities.

Provides static methods for generating and validating JWT tokens used for authentication and authorization.

auto app = MantisBase::create();
auto& auth = app->auth();
json claims = {{"id", "user123"}, {"table", "users"}};
std::string token = auth.createToken(claims, 3600); // 1 hour
json result = auth.verifyToken(token);
if (result["verified"].get<bool>()) {
std::string userId = result["id"];
}
// API keys and OAuth live on the same MbAuth instance:
auth.apiKey().create("users", "user123", "Mobile app");
auth.oauth().buildAuthorizeUrl("users", "google", redirect_uri);
static std::unique_ptr< MantisBase > create(int argc, char **argv)
Create class instance given cmd args passed in.
Definition mantisbase.cpp:75
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36

Constructor & Destructor Documentation

◆ MbAuth()

mb::MbAuth::MbAuth ( const MantisBase & app)
explicit

Construct auth services bound to app (OAuth + API keys).

Member Function Documentation

◆ apiKey()

MbApiKeyMgr & mb::MbAuth::apiKey ( ) const
nodiscard
Returns
API key manager for programmatic credentials.

◆ confirmEmailVerification()

json mb::MbAuth::confirmEmailVerification ( const std::string & entity_name,
const std::string & token ) const
nodiscard

Mark the account verified when token matches a live verification link.

Returns
{ "verified": true }.
Exceptions
MbException400 when the token is missing, unknown, or expired.

◆ confirmPasswordReset()

json mb::MbAuth::confirmPasswordReset ( const std::string & entity_name,
const std::string & token,
const std::string & new_password ) const
nodiscard

Set a new password when token matches a live reset link.

Returns
{ "reset": true }.
Exceptions
MbException400 when the token or password is invalid/expired.

◆ createToken()

std::string mb::MbAuth::createToken ( const json & claims_params,
int timeout = -1 ) const
nodiscard

Create JWT token with custom claims.

Parameters
claims_paramsJSON object with claims (must include "id" and "table")
timeoutToken expiration in seconds (-1 for default, typically 1 hour)
Returns
JWT token string
std::string token = app.auth().createToken(
{{"id", "user123"}, {"table", "users"}}, 3600);

◆ deleteSession()

bool mb::MbAuth::deleteSession ( const std::string & session_id) const
nodiscard

Invalidate a refresh/session row by id.

◆ loginWithOtp()

json mb::MbAuth::loginWithOtp ( const std::string & entity_name,
const std::string & email,
const std::string & otp ) const
nodiscard

Exchange a one-time sign-in code for a session token.

Passwordless accounts (for example OAuth-only users) can sign in this way. The code is single-use.

Parameters
entity_nameAuth entity table name.
emailAccount email address.
otp6-digit code from the sign-in email.
Returns
JSON with token and user (password omitted).
Exceptions
MbException400 when the email or code is not in the expected shape.
MbException401 when the code is wrong, expired, used, or exhausted.

◆ notifyEmailVerification()

void mb::MbAuth::notifyEmailVerification ( const std::string & entity_name,
const std::string & user_id ) const

Email a verification link when SMTP is configured and the account is unverified. Missing SMTP or siteDomain is logged and ignored so registration can still succeed.

◆ oauth()

MbOAuthMgr & mb::MbAuth::oauth ( ) const
nodiscard
Returns
OAuth manager for provider configuration and login flows.

◆ refreshSession()

json mb::MbAuth::refreshSession ( const std::string & old_session_id,
const std::string & entity_name,
const std::string & user_id )

Rotate a session: invalidate old_session_id and issue a new JWT.

Returns
JSON with new token on success.

◆ rejectUnverifiedLogin()

void mb::MbAuth::rejectUnverifiedLogin ( const std::string & entity_name,
const json & user ) const

Reject login when the entity requires a verified email and the account is not verified.

Exceptions
MbException403 when verification is required and email_verified is not true.

◆ requestEmailVerification()

json mb::MbAuth::requestEmailVerification ( const std::string & entity_name,
const std::string & email,
const std::string & request_host ) const
nodiscard

Send a verification link for an email address. Unknown addresses return { "sent": true } and send nothing.

Parameters
request_hostHost header, used only when siteDomain is empty.
Exceptions
MbException400 when SMTP is not configured, the address is invalid, or no public base URL is set.

◆ requestLoginOtp()

json mb::MbAuth::requestLoginOtp ( const std::string & entity_name,
const std::string & email ) const
nodiscard

Email a one-time sign-in code for an auth-entity account.

The code expires after 10 minutes and allows 5 attempts. A successfully delivered code replaces any earlier code for that account. When the address is valid, unknown emails return { "sent": true } and no message is sent. A message is delivered only when an account with that email exists.

Parameters
entity_nameAuth entity table name.
emailAccount email address.
Returns
JSON object { "sent": true }.
Exceptions
MbException400 when SMTP is not configured or the address is invalid.
MbExceptionwhen delivery fails (rate limit or SMTP error).

◆ requestPasswordReset()

json mb::MbAuth::requestPasswordReset ( const std::string & entity_name,
const std::string & email,
const std::string & request_host ) const
nodiscard

Email a password-reset link for an account. Unknown addresses return { "sent": true } and send nothing.

Parameters
request_hostHost header, used only when siteDomain is empty.
Exceptions
MbException400 when SMTP is not configured, the address is invalid, or no public base URL is set.

◆ sessionTimeoutSeconds()

int mb::MbAuth::sessionTimeoutSeconds ( const std::string & entity_name,
int timeout = -1 ) const
nodiscard

Default session lifetime in seconds for an entity (or timeout when positive).

◆ verifyToken()

json mb::MbAuth::verifyToken ( const std::string & token) const
nodiscard

Verify a JWT and return claims plus verification metadata.

Returns
JSON with verified, error, and claim fields on success/failure.

The documentation for this class was generated from the following files:
  • /home/runner/work/mantisbase/mantisbase/include/mantisbase/core/auth.h
  • /home/runner/work/mantisbase/mantisbase/src/core/auth.cpp