-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support requiring 2FA for authentication, checked with
isEnrolledIn2Sv
Using Google's Admin SDK Directory API and retrieving the User entity when, we can check the `isEnrolledIn2Sv` field on https://developers.google.com/admin-sdk/directory/reference/rest/v1/users to accurately obtain the 2FA status of a user. At the Guardian, in the past, we've checked the user for membership of the `2fa_enforce@guardian.co.uk` Google Group, as a proxy for being able to directly check the 2FA status of the user. The Google Group was manually administered, so suffered from reconciliation issues, and no longer corresponds to our onboarding process. Now, if you supply a `TwoFactorAuthChecker` to the `GoogleAuthConfig`, the 2FA status of the user will be checked at the point of authentication, and rejected if `isEnrolledIn2Sv` is false - only users with 2FA will be allowed to authenticate.
- Loading branch information
Showing
6 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
play-v27/src/main/scala/com/gu/googleauth/TwoFactorAuthChecker.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.gu.googleauth | ||
|
||
import com.google.api.services.directory.DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY | ||
import com.google.auth.oauth2.GoogleCredentials | ||
import com.gu.googleauth.internal.DirectoryService | ||
|
||
import scala.concurrent.{ExecutionContext, Future, blocking} | ||
|
||
/** | ||
* Uses the `isEnrolledIn2Sv` field on https://developers.google.com/admin-sdk/directory/reference/rest/v1/users | ||
* to check the 2FA status of a user. | ||
* | ||
* @param googleCredentials must have read-only access to retrieve a User using the Admin SDK Directory API | ||
*/ | ||
class TwoFactorAuthChecker(googleCredentials: GoogleCredentials) { | ||
|
||
private val usersApi = DirectoryService(googleCredentials, ADMIN_DIRECTORY_USER_READONLY).users() | ||
|
||
def check(userEmail: String)(implicit ec: ExecutionContext): Future[Boolean] = Future { blocking { | ||
usersApi.get(userEmail).execute().getIsEnrolledIn2Sv | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
play-v27/src/test/scala/com/gu/googleauth/TwoFactorAuthCheckerCLITester.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.gu.googleauth | ||
|
||
import com.google.auth.oauth2.GoogleCredentials | ||
import software.amazon.awssdk.auth.credentials.{AwsCredentialsProvider, ProfileCredentialsProvider} | ||
import software.amazon.awssdk.http.apache.ApacheHttpClient | ||
import software.amazon.awssdk.regions.Region.EU_WEST_1 | ||
import software.amazon.awssdk.services.ssm.SsmClient | ||
import software.amazon.awssdk.services.ssm.model.GetParameterRequest | ||
|
||
import scala.concurrent.Await | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.duration.Duration | ||
|
||
/** | ||
* This is a non-production piece of code, only intended for checking that Google Credentials are correctly set up, | ||
* and that we're able to get a good response from the Google Admin SDK Directory API. | ||
*/ | ||
object TwoFactorAuthCheckerCLITester extends App { | ||
|
||
val checker = new TwoFactorAuthChecker(GoogleCredentialsInDev.loadGuardianGoogleCredentials()) | ||
|
||
val userEmail = "roberto.tyley@guardian.co.uk" | ||
|
||
val has2FA: Boolean = Await.result(checker.check(userEmail), Duration.Inf) | ||
|
||
println(s"User $userEmail has 2FA enabled: $has2FA") | ||
} | ||
|
||
object GoogleCredentialsInDev { | ||
/** | ||
* Only intended for use by Guardian developers, in development. Requires Janus credentials. | ||
*/ | ||
def loadGuardianGoogleCredentials(): GoogleCredentials = { | ||
val aws = new AWS("ophan") | ||
val serviceAccountCert = aws.loadSecureString("/Ophan/Dashboard/GoogleCloudPlatform/OphanOAuthServiceAccountCert") | ||
val impersonatedUser = aws.loadSecureString("/Ophan/Dashboard/GoogleCloudPlatform/ImpersonatedUser") | ||
|
||
ServiceAccountHelper.credentialsFrom(serviceAccountCert).createDelegated(impersonatedUser) | ||
} | ||
} | ||
|
||
class AWS(profileName: String) { | ||
val credentials: AwsCredentialsProvider = ProfileCredentialsProvider.builder().profileName(profileName).build() | ||
|
||
val SSM: SsmClient = | ||
SsmClient.builder().httpClientBuilder(ApacheHttpClient.builder()).credentialsProvider(credentials).region(EU_WEST_1).build() | ||
|
||
def loadSecureString(paramName: String): String = SSM.getParameter( | ||
GetParameterRequest.builder().name(paramName).withDecryption(true).build() | ||
).parameter().value() | ||
} |