-
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.
- Loading branch information
Showing
5 changed files
with
88 additions
and
1 deletion.
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
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() | ||
} |