Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/self-host/email.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ A self-hosted install sends the emails it needs, and nothing else until you turn
| Support ticket updates | Essential | Replies and status changes on tickets someone filed. |
| Webhook endpoint turned off | Essential | Tells a team's admins when CoderHelm stopped sending to an endpoint that failed for days. |
| Two-factor lockout alerts | Essential | Tells admins and owners when someone's two-factor sign-in was paused after too many wrong codes. |
| Two-factor sign-in required | Essential | Tells people without a second factor that an admin now requires one, before their next sign-in is held. |
| Two-factor sign-in reset | Essential | Tells a person an admin reset their second factor, so a reset they didn't ask for doesn't go unnoticed. |
| Update notices | Essential | New releases, finished or failed updates, address changes, to install admins. |
| Test email | Essential | Only when an admin presses **Send test email** in Setup. |
| Weekly summary | Optional, **off** | The Monday email with each team's week. |
Expand Down
4 changes: 3 additions & 1 deletion docs/self-host/sign-in.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ An install from before this change keeps working: its oldest team becomes the or

Anyone can turn on two-factor sign-in for themselves in **Settings, Security**. Admins can require it for everyone: **Settings, Sign-in, Require two-factor sign-in**. Turn it on for yourself first; the switch asks for that, so you can't lock yourself out.

When you turn it on, everyone who has no second factor yet gets an email saying so, with a link to set it up (Deliveries shows each one). **Settings, Sign-in** shows how many people haven't set it up, before and after.

With it on:

- **Everyone** must set up an authenticator app (any TOTP app: 1Password, Google Authenticator, Authy and so on) before they can use CoderHelm. At their next sign-in they see a setup screen with a code to scan, and nothing else works until they enter a code from the app.
Expand All @@ -50,7 +52,7 @@ Email and password, Google and GitHub all lead to the same person in CoderHelm (
- **Cognito's code on the password account**: only Cognito can check it, so it is asked for at the password sign-in. Signing in with Google or GitHub sends the person back to sign in with their email and password.
- **Neither kind can be set up while the person has one**, from any session, including one held at the setup screen. To change it, turn the current one off first: the app code takes a current code to turn off, and the password account's code can be turned off only from a session that passed it. If someone lost their phone, an admin resets it (below).

**Settings, Sign-in** shows who has set it up. If someone loses their phone, an admin can reset their two-factor sign-in after checking it's really them (a video call, say). The admin writes how they checked; the reset and the reason go in the log under **Settings, Roles**. The person sets it up again at their next sign-in. Only an owner can reset an admin's or another owner's. Nobody can reset their own.
**Settings, Sign-in** shows who has set it up. If someone loses their phone, an admin can reset their two-factor sign-in after checking it's really them (a video call, say). The admin writes how they checked; the reset and the reason go in the log under **Settings, Roles**. The person sets it up again at their next sign-in, and gets an email saying their two-factor sign-in was reset, so a reset they didn't ask for doesn't go unnoticed. Only an owner can reset an admin's or another owner's. Nobody can reset their own.

### Wrong codes

Expand Down
2 changes: 2 additions & 0 deletions services/common/src/mail/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ pub const ESSENTIAL: &[EssentialEmail] = &[
EssentialEmail { id: "support_update", label: "Support ticket updates", why: "Replies and status changes on tickets someone filed." },
EssentialEmail { id: "webhook_disabled", label: "Webhook endpoint turned off", why: "Tells a team's admins when CoderHelm stopped sending to an endpoint that failed for days." },
EssentialEmail { id: "two_factor_lockout", label: "Two-factor lockout alerts", why: "Tells admins and owners when someone's two-factor sign-in was paused after too many wrong codes." },
EssentialEmail { id: "two_factor_required", label: "Two-factor sign-in required", why: "Tells people without a second factor that an admin now requires one, before their next sign-in is held." },
EssentialEmail { id: "two_factor_reset", label: "Two-factor sign-in reset", why: "Tells a person an admin reset their second factor, so a reset they didn't ask for doesn't go unnoticed." },
EssentialEmail { id: "update_notice", label: "Update notices", why: "Tells install admins about new releases, finished or failed updates and address changes." },
EssentialEmail { id: "test_email", label: "Test email", why: "Sent only when an admin presses Send test email in Setup." },
];
Expand Down
21 changes: 21 additions & 0 deletions services/common/src/mail/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,27 @@ pub const TEMPLATES: &[Template] = &[
],
install_may_send: true,
},
Template {
id: "two_factor_required",
subject: "Two-factor sign-in is now required",
// No team or admin name: the install's own SES email carries those.
params: &[p("link", Kind::Link)],
body: &[
Part::Para("Two-factor sign-in is now required for you. The next time you sign in, you'll set up an authenticator app on your phone first (it takes about a minute); nothing else works until then."),
Part::Button("Set it up now", "link"),
],
install_may_send: true,
},
Template {
id: "two_factor_reset",
subject: "Your two-factor sign-in was reset",
params: &[p("link", Kind::Link)],
body: &[
Part::Para("An admin reset your two-factor sign-in. Set it up again at your next sign-in. If you didn't ask for this, tell your admins and change your password now."),
Part::Button("Open Security settings", "link"),
],
install_may_send: true,
},
// The relay's own notice to an install's admins. Installs can't ask for it.
Template {
id: "relay_paused",
Expand Down
15 changes: 7 additions & 8 deletions services/gateway/src/middleware/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,20 @@ pub async fn require_auth(
.extensions()
.get::<MatchedPath>()
.and_then(|m| permissions::routed_handler(req.method().as_str(), m.as_str()));
// How this session signed in. On a self-hosted install that requires
// two-factor sign-in, a session without a second factor reaches only the
// screens that set one up; the rule is read on every request, so sessions
// from before it was turned on are held at their next request.
// How this session signed in. When the team it acts in (a self-hosted
// install: the organization) requires two-factor sign-in, a session
// without a second factor reaches only the screens that set one up; the
// rule is read on every request, so sessions from before it was turned
// on are held at their next request.
let proof = jwt::read_proof(token, &state.secrets.jwt_secret);
// The session against the member's record, read on every request: a
// removal, a role change, a two-factor reset or turning two-factor off
// ends every session issued before it.
if let Err(ended) = sign_in::check_member_session(&state, &claims).await {
return Ok(ended);
}
if sign_in::applies() {
if let Err(held) = sign_in::check_session(&state, &proof, handler).await {
return Ok(held);
}
if let Err(held) = sign_in::check_session(&state, &claims.team_id, &proof, handler).await {
return Ok(held);
}

let holder = roles::holder_for(&state, &claims).await;
Expand Down
2 changes: 1 addition & 1 deletion services/gateway/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ pub const CAPABILITIES: &[Capability] = &[
},
Capability {
key: "sign_in",
label: "Change who may join (allowed email domains) and whether two-factor sign-in is required, see who has it, and reset a person's two-factor sign-in (self-hosted installs)",
label: "Require two-factor sign-in for the team, see who has it, reset a person's two-factor sign-in, and on a self-hosted install change who may join (allowed email domains)",
min_role: "admin",
// Sign-in rules stay with built-in admins and owners
// (`NEVER_IN_CUSTOM_ROLES`).
Expand Down
21 changes: 9 additions & 12 deletions services/gateway/src/routes/api_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,15 @@ pub async fn list(
})
})
.collect();
// With two-factor sign-in required: the keys made before the rule, and
// whether their maker has a second factor now (a key never signs in).
let reviews = if super::sign_in::applies() {
super::sign_in::key_reviews(&state, &claims.team_id, &all)
.await
.unwrap_or_else(|e| {
error!("API key two-factor review not read: {e}");
Default::default()
})
} else {
Default::default()
};
// With two-factor sign-in required (the team's rule; a self-hosted
// install's organization): the keys made before the rule, and whether
// their maker has a second factor now (a key never signs in).
let reviews = super::sign_in::key_reviews(&state, &claims.team_id, &all)
.await
.unwrap_or_else(|e| {
error!("API key two-factor review not read: {e}");
Default::default()
});
let views: Vec<Value> = all
.iter()
.map(|k| {
Expand Down
101 changes: 75 additions & 26 deletions services/gateway/src/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,15 @@ pub async fn mfa_verify(
&SignInProof::new(true, "password"),
)
.await?;
if sign_in::applies() && resp.status().is_success() {
if resp.status().is_success() {
let sub = format!("USER#{}", user.username());
if let Some(team) = cognito_member_team(&state, &email, &sub).await {
sign_in::note_factor(&state, &team, &sub, Some("cognito")).await;
if sign_in::applies() {
if let Some(team) = cognito_member_team(&state, &email, &sub).await {
sign_in::note_factor(&state, &team, &sub, &email, Some("cognito")).await;
}
} else {
// Hosted: the person's own record, by email.
sign_in::note_factor(&state, "", &sub, &email, Some("cognito")).await;
}
}
Ok(resp)
Expand Down Expand Up @@ -1064,19 +1069,38 @@ async fn google_callback_inner(
(tid, uid, "owner".to_string())
};

// Issue JWT
let token = jwt::create_token(
// The person's second factor, whichever it is, comes before a session,
// whether or not a team requires it.
let dashboard_url = common::public_urls::app_url(&state.config.stage);
let step = SecondStep {
sub: user_id.clone(),
team_id: team_id.clone(),
email: email.clone(),
role: role.clone(),
github_login: None,
via: "google".into(),
jti: String::new(),
aud: String::new(),
iat: 0,
exp: 0,
};
if let Some(resp) = sign_in::second_step(state, step, dashboard_url)
.await
.map_err(|e| SignInFailure::new(P, Reason::Server, "second_step", e))?
{
return Ok(resp);
}
let token = jwt::create_session_token(
&user_id,
&team_id,
&email,
&role,
None,
&SignInProof::new(false, "google"),
&state.secrets.jwt_secret,
SESSION_TTL_SECS,
)
.map_err(|e| SignInFailure::new(P, Reason::Server, "session_token", e))?;

let dashboard_url = common::public_urls::app_url(&state.config.stage);
Ok(session_redirect(P, &token, dashboard_url))
}

Expand Down Expand Up @@ -1648,25 +1672,27 @@ async fn github_callback_inner(

if sign_in::applies() {
touch_last_login(state, &team_id, &user_id).await;
let step = SecondStep {
sub: user_id.clone(),
team_id: team_id.clone(),
email: email.to_string(),
role: role.clone(),
github_login: Some(github_login.to_string()),
via: "github".into(),
jti: String::new(),
aud: String::new(),
iat: 0,
exp: 0,
};
let dash = common::public_urls::app_url(&state.config.stage);
if let Some(resp) = sign_in::second_step(state, step, dash)
.await
.map_err(|e| SignInFailure::new(P, Reason::Server, "second_step", e))?
{
return Ok(resp);
}
}
// The person's second factor, whichever it is, comes before a session,
// on every deployment and whether or not a team requires it.
let step = SecondStep {
sub: user_id.clone(),
team_id: team_id.clone(),
email: email.to_string(),
role: role.clone(),
github_login: Some(github_login.to_string()),
via: "github".into(),
jti: String::new(),
aud: String::new(),
iat: 0,
exp: 0,
};
let dash = common::public_urls::app_url(&state.config.stage);
if let Some(resp) = sign_in::second_step(state, step, dash)
.await
.map_err(|e| SignInFailure::new(P, Reason::Server, "second_step", e))?
{
return Ok(resp);
}

let token = jwt::create_session_token(
Expand Down Expand Up @@ -2021,6 +2047,29 @@ async fn issue_session_from_cognito(
};

touch_last_login(state, &team_id, &user_id).await;
// The person's second factor, whichever it is, comes before a session,
// whether or not a team requires it.
let step = SecondStep {
sub: user_id.clone(),
team_id: team_id.clone(),
email: email.to_string(),
role: role.clone(),
github_login: github_login.clone(),
via: "password".into(),
jti: String::new(),
aud: String::new(),
iat: 0,
exp: 0,
};
if let Some(resp) = sign_in::password_step(state, step, proof.mfa)
.await
.map_err(|e| {
error!("Second factor not read: {e}");
StatusCode::SERVICE_UNAVAILABLE
})?
{
return Ok(resp);
}
session_json(
state,
&team_id,
Expand Down
Loading
Loading