Skip to content

Commit c63274f

Browse files
authored
Merge pull request #747 from code0-tech/689-expose-mfa-status-for-admins-and-self-user
Expose mfa status for admin and self-users
2 parents 83a042e + a469ddd commit c63274f

File tree

7 files changed

+65
-0
lines changed

7 files changed

+65
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module Types
4+
class MfaStatusType < Types::BaseObject
5+
description 'Represents the MFA status of a user'
6+
7+
authorize :read_mfa_status
8+
9+
field :enabled, Boolean, null: false,
10+
description: 'Indicates whether MFA is enabled for the user.'
11+
12+
field :totp_enabled, Boolean, null: false,
13+
description: 'Indicates whether TOTP MFA is enabled for the user.'
14+
15+
field :backup_codes_count, Integer, null: false,
16+
description: 'The number of backup codes remaining for the user.'
17+
end
18+
end

app/graphql/types/user_type.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class UserType < Types::BaseObject
4141
description: 'Identities of this user',
4242
method: :user_identities
4343

44+
field :mfa_status, Types::MfaStatusType,
45+
null: true,
46+
description: 'Multi-factor authentication status of this user'
47+
4448
lookahead_field :namespace_memberships,
4549
base_scope: ->(object) { object.namespace_memberships },
4650
conditional_lookaheads: { user: :user, namespace: { namespace: :namespace_members } }
@@ -58,5 +62,13 @@ def avatar_path
5862

5963
Rails.application.routes.url_helpers.rails_storage_proxy_path object.avatar
6064
end
65+
66+
def mfa_status
67+
{
68+
enabled: object.mfa_enabled?,
69+
totp_enabled: object.totp_secret.present?,
70+
backup_codes_count: object.backup_codes.size,
71+
}
72+
end
6173
end
6274
end

app/policies/user_policy.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class UserPolicy < BasePolicy
1414
enable :read_email
1515
enable :delete_user
1616
enable :read_admin_status
17+
enable :read_mfa_status
1718
end
1819

1920
rule { admin_status_visible & ~anonymous }.enable :read_admin_status
@@ -26,5 +27,6 @@ class UserPolicy < BasePolicy
2627
enable :verify_email
2728
enable :send_verification_email
2829
enable :read_email
30+
enable :read_mfa_status
2931
end
3032
end

docs/graphql/object/mfastatus.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: MfaStatus
3+
---
4+
5+
Represents the MFA status of a user
6+
7+
## Fields without arguments
8+
9+
| Name | Type | Description |
10+
|------|------|-------------|
11+
| `backupCodesCount` | [`Int!`](../scalar/int.md) | The number of backup codes remaining for the user. |
12+
| `enabled` | [`Boolean!`](../scalar/boolean.md) | Indicates whether MFA is enabled for the user. |
13+
| `totpEnabled` | [`Boolean!`](../scalar/boolean.md) | Indicates whether TOTP MFA is enabled for the user. |
14+

docs/graphql/object/user.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Represents a user
1717
| `id` | [`UserID!`](../scalar/userid.md) | Global ID of this User |
1818
| `identities` | [`UserIdentityConnection!`](../object/useridentityconnection.md) | Identities of this user |
1919
| `lastname` | [`String`](../scalar/string.md) | Lastname of the user |
20+
| `mfaStatus` | [`MfaStatus`](../object/mfastatus.md) | Multi-factor authentication status of this user |
2021
| `namespace` | [`Namespace`](../object/namespace.md) | Namespace of this user |
2122
| `namespaceMemberships` | [`NamespaceMemberConnection!`](../object/namespacememberconnection.md) | Namespace Memberships of this user |
2223
| `sessions` | [`UserSessionConnection!`](../object/usersessionconnection.md) | Sessions of this user |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe SagittariusSchema.types['MfaStatus'] do
6+
let(:fields) do
7+
%w[
8+
enabled
9+
totpEnabled
10+
backupCodesCount
11+
]
12+
end
13+
14+
it { expect(described_class.graphql_name).to eq('MfaStatus') }
15+
it { expect(described_class).to have_graphql_fields(fields) }
16+
it { expect(described_class).to require_graphql_authorizations(:read_mfa_status) }
17+
end

spec/graphql/types/user_type_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
emailVerifiedAt
1818
sessions
1919
identities
20+
mfaStatus
2021
userAbilities
2122
createdAt
2223
updatedAt

0 commit comments

Comments
 (0)