-
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.
Merge pull request #81 from guardian/remove-use-of-deprecated-googlec…
…redentials-class Remove use of deprecated `GoogleCredential` class
- Loading branch information
Showing
5 changed files
with
50 additions
and
34 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,11 @@ package com.gu.googleauth | |
|
||
import java.security.PrivateKey | ||
|
||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential | ||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport | ||
import com.google.api.client.json.jackson2.JacksonFactory | ||
import com.google.api.services.directory.{Directory, DirectoryScopes} | ||
import com.google.auth.http.HttpCredentialsAdapter | ||
import com.google.auth.oauth2.ServiceAccountCredentials | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.concurrent._ | ||
|
@@ -22,6 +23,7 @@ import scala.concurrent._ | |
* @param privateKey the Service Account's private key - from the P12 file generated when the Service Account was created | ||
* @param impersonatedUser the email address of the user the application will be impersonating | ||
*/ | ||
@deprecated("Use com.google.auth.oauth2.ServiceAccountCredentials instead", "play-googleauth 2.1.0") | ||
case class GoogleServiceAccount( | ||
email: String, | ||
privateKey: PrivateKey, | ||
|
@@ -35,25 +37,44 @@ case class GoogleServiceAccount( | |
* doesn't seem to work?). The Service Account needs the following scope: | ||
* https://www.googleapis.com/auth/admin.directory.group.readonly | ||
* | ||
* You also need a separate domain user account (eg [email protected]), which | ||
* will be 'impersonated' when making the calls. | ||
* So long as you have the Service Account certificate as a string, you can easily make | ||
* an instance of com.google.auth.oauth2.ServiceAccountCredentials like this: | ||
* | ||
* {{{ | ||
* import org.apache.commons.io.Charsets.UTF_8 | ||
* import org.apache.commons.io.IOUtils | ||
* import com.google.auth.oauth2.ServiceAccountCredentials | ||
* | ||
* val serviceAccountCert: String = ... // certificate from Google Developers Console | ||
* val credentials = ServiceAccountCredentials.fromStream(IOUtils.toInputStream(serviceAccountCert, UTF_8)) | ||
* }}} | ||
* | ||
* @param impersonatedUser a separate domain-user account email address (eg '[email protected]'), the email address | ||
* of the user the application will be impersonating when making calls. | ||
*/ | ||
class GoogleGroupChecker(directoryServiceAccount: GoogleServiceAccount) { | ||
class GoogleGroupChecker(impersonatedUser: String, serviceAccountCredentials: ServiceAccountCredentials) { | ||
|
||
val directoryService = { | ||
@deprecated( | ||
"this constructor is deprecated, use the constructor accepting com.google.auth.oauth2.ServiceAccountCredentials instead", | ||
"play-googleauth 2.1.0" | ||
) | ||
def this(googleServiceAccount: GoogleServiceAccount) = { | ||
this( | ||
googleServiceAccount.impersonatedUser, | ||
ServiceAccountCredentials.newBuilder() | ||
.setPrivateKey(googleServiceAccount.privateKey) | ||
.setServiceAccountUser(googleServiceAccount.email) | ||
.build() | ||
) | ||
} | ||
|
||
val directoryService: Directory = { | ||
val credentials = serviceAccountCredentials | ||
.createDelegated(impersonatedUser) | ||
.createScoped(DirectoryScopes.ADMIN_DIRECTORY_GROUP_READONLY) | ||
val transport = GoogleNetHttpTransport.newTrustedTransport() | ||
val jsonFactory = JacksonFactory.getDefaultInstance | ||
|
||
val credential = new GoogleCredential.Builder() | ||
.setTransport(transport) | ||
.setJsonFactory(jsonFactory) | ||
.setServiceAccountId(directoryServiceAccount.email) | ||
.setServiceAccountUser(directoryServiceAccount.impersonatedUser) | ||
.setServiceAccountPrivateKey(directoryServiceAccount.privateKey) | ||
.setServiceAccountScopes(Seq(DirectoryScopes.ADMIN_DIRECTORY_GROUP_READONLY).asJava) | ||
.build() | ||
|
||
new Directory.Builder(transport, jsonFactory, null).setHttpRequestInitializer(credential).build | ||
new Directory.Builder(transport, jsonFactory, new HttpCredentialsAdapter(credentials)).build | ||
} | ||
|
||
def retrieveGroupsFor(userEmail: String)(implicit ec: ExecutionContext): Future[Set[String]] = for { | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version in ThisBuild := "2.0.3-SNAPSHOT" | ||
version in ThisBuild := "2.1.0-SNAPSHOT" |