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
1 parent
636c8ba
commit 9aceaa7
Showing
9 changed files
with
207 additions
and
8 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
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
41 changes: 41 additions & 0 deletions
41
...src/main/java/org/whispersystems/textsecuregcm/configuration/ArtServiceConfiguration.java
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,41 @@ | ||
/* | ||
* Copyright 2022 Signal Messenger, LLC | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
package org.whispersystems.textsecuregcm.configuration; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import org.apache.commons.codec.DecoderException; | ||
import org.apache.commons.codec.binary.Hex; | ||
|
||
import java.time.Duration; | ||
import javax.validation.constraints.NotEmpty; | ||
import javax.validation.constraints.NotNull; | ||
|
||
public class ArtServiceConfiguration { | ||
|
||
@NotEmpty | ||
@JsonProperty | ||
private String userAuthenticationTokenSharedSecret; | ||
|
||
@NotEmpty | ||
@JsonProperty | ||
private String userAuthenticationTokenUserIdSecret; | ||
|
||
@JsonProperty | ||
@NotNull | ||
private Duration tokenExpiration = Duration.ofDays(1); | ||
|
||
public byte[] getUserAuthenticationTokenSharedSecret() throws DecoderException { | ||
return Hex.decodeHex(userAuthenticationTokenSharedSecret.toCharArray()); | ||
} | ||
|
||
public byte[] getUserAuthenticationTokenUserIdSecret() throws DecoderException { | ||
return Hex.decodeHex(userAuthenticationTokenUserIdSecret.toCharArray()); | ||
} | ||
|
||
public Duration getTokenExpiration() { | ||
return tokenExpiration; | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
service/src/main/java/org/whispersystems/textsecuregcm/controllers/ArtController.java
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,41 @@ | ||
/* | ||
* Copyright 2013-2022 Signal Messenger, LLC | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
package org.whispersystems.textsecuregcm.controllers; | ||
|
||
import com.codahale.metrics.annotation.Timed; | ||
import io.dropwizard.auth.Auth; | ||
import java.util.UUID; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import org.whispersystems.textsecuregcm.auth.AuthenticatedAccount; | ||
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialGenerator; | ||
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentials; | ||
import org.whispersystems.textsecuregcm.limits.RateLimiters; | ||
|
||
@Path("/v1/art") | ||
public class ArtController { | ||
private final ExternalServiceCredentialGenerator artServiceCredentialGenerator; | ||
private final RateLimiters rateLimiters; | ||
|
||
public ArtController(RateLimiters rateLimiters, | ||
ExternalServiceCredentialGenerator artServiceCredentialGenerator) { | ||
this.artServiceCredentialGenerator = artServiceCredentialGenerator; | ||
this.rateLimiters = rateLimiters; | ||
} | ||
|
||
@Timed | ||
@GET | ||
@Path("/auth") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public ExternalServiceCredentials getAuth(@Auth AuthenticatedAccount auth) | ||
throws RateLimitExceededException { | ||
final UUID uuid = auth.getAccount().getUuid(); | ||
rateLimiters.getArtPackLimiter().validate(uuid); | ||
return artServiceCredentialGenerator.generateFor(uuid.toString()); | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
...e/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/ArtControllerTest.java
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,60 @@ | ||
/* | ||
* Copyright 2013-2021 Signal Messenger, LLC | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
package org.whispersystems.textsecuregcm.tests.controllers; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import io.dropwizard.auth.PolymorphicAuthValueFactoryProvider; | ||
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport; | ||
import io.dropwizard.testing.junit5.ResourceExtension; | ||
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.whispersystems.textsecuregcm.auth.AuthenticatedAccount; | ||
import org.whispersystems.textsecuregcm.auth.DisabledPermittedAuthenticatedAccount; | ||
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialGenerator; | ||
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentials; | ||
import org.whispersystems.textsecuregcm.controllers.ArtController; | ||
import org.whispersystems.textsecuregcm.limits.RateLimiter; | ||
import org.whispersystems.textsecuregcm.limits.RateLimiters; | ||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper; | ||
import org.whispersystems.textsecuregcm.util.SystemMapper; | ||
|
||
@ExtendWith(DropwizardExtensionsSupport.class) | ||
class ArtControllerTest { | ||
|
||
private static final ExternalServiceCredentialGenerator artCredentialGenerator = new ExternalServiceCredentialGenerator( | ||
new byte[32], new byte[32], true, false, false); | ||
private static final RateLimiter rateLimiter = mock(RateLimiter.class); | ||
private static final RateLimiters rateLimiters = mock(RateLimiters.class); | ||
|
||
private static final ResourceExtension resources = ResourceExtension.builder() | ||
.addProvider(AuthHelper.getAuthFilter()) | ||
.addProvider(new PolymorphicAuthValueFactoryProvider.Binder<>( | ||
ImmutableSet.of(AuthenticatedAccount.class, DisabledPermittedAuthenticatedAccount.class))) | ||
.setMapper(SystemMapper.getMapper()) | ||
.setTestContainerFactory(new GrizzlyWebTestContainerFactory()) | ||
.addResource(new ArtController(rateLimiters, artCredentialGenerator)) | ||
.build(); | ||
|
||
@Test | ||
void testGetAuthToken() { | ||
when(rateLimiters.getArtPackLimiter()).thenReturn(rateLimiter); | ||
|
||
ExternalServiceCredentials token = | ||
resources.getJerseyTest() | ||
.target("/v1/art/auth") | ||
.request() | ||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)) | ||
.get(ExternalServiceCredentials.class); | ||
|
||
assertThat(token.getPassword()).isNotEmpty(); | ||
assertThat(token.getUsername()).isNotEmpty(); | ||
} | ||
} |