forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify holder of the device code (#21)
Closes keycloak/security#32 Co-authored-by: Stian Thorgersen <[email protected]> Conflicts: services/src/main/java/org/keycloak/protocol/oidc/grants/device/DeviceGrantType.java
- Loading branch information
Showing
5 changed files
with
133 additions
and
11 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
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
*/ | ||
package org.keycloak.testsuite.oauth; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.keycloak.models.OAuth2DeviceConfig.DEFAULT_OAUTH2_DEVICE_CODE_LIFESPAN; | ||
|
@@ -59,11 +60,13 @@ | |
import org.apache.http.message.BasicNameValuePair; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.keycloak.util.BasicAuthHelper; | ||
import org.openqa.selenium.Cookie; | ||
|
||
import java.util.List; | ||
import java.util.LinkedList; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Hiroyuki Wada</a> | ||
|
@@ -106,6 +109,7 @@ public void addTestRealms(List<RealmRepresentation> testRealms) { | |
|
||
ClientRepresentation appPublic = ClientBuilder.create().id(KeycloakModelUtils.generateId()).publicClient() | ||
.clientId(DEVICE_APP_PUBLIC).attribute(OAuth2DeviceConfig.OAUTH2_DEVICE_AUTHORIZATION_GRANT_ENABLED, "true") | ||
.redirectUris(OAuthClient.APP_ROOT + "/auth") | ||
.build(); | ||
realm.client(appPublic); | ||
|
||
|
@@ -243,6 +247,72 @@ public void testCustomVerificationUri() throws Exception { | |
} | ||
} | ||
|
||
@Test | ||
public void testVerifyHolderOfDeviceCode() throws Exception { | ||
// Device Authorization Request from device | ||
oauth.realm(REALM_NAME); | ||
oauth.clientId(DEVICE_APP_PUBLIC); | ||
OAuthClient.DeviceAuthorizationResponse response = oauth.doDeviceAuthorizationRequest(DEVICE_APP_PUBLIC, null); | ||
|
||
assertEquals(200, response.getStatusCode()); | ||
assertNotNull(response.getDeviceCode()); | ||
assertNotNull(response.getUserCode()); | ||
assertNotNull(response.getVerificationUri()); | ||
assertNotNull(response.getVerificationUriComplete()); | ||
assertEquals(60, response.getExpiresIn()); | ||
assertEquals(5, response.getInterval()); | ||
|
||
openVerificationPage(response.getVerificationUriComplete()); | ||
|
||
// Do Login | ||
oauth.fillLoginForm("device-login", "password"); | ||
|
||
// Consent | ||
grantPage.accept(); | ||
|
||
// Token request from device | ||
OAuthClient.AccessTokenResponse tokenResponse = oauth.doDeviceTokenRequest(DEVICE_APP_PUBLIC, null, response.getDeviceCode()); | ||
|
||
assertEquals(200, tokenResponse.getStatusCode()); | ||
|
||
String tokenString = tokenResponse.getAccessToken(); | ||
assertNotNull(tokenString); | ||
AccessToken token = oauth.verifyToken(tokenString); | ||
|
||
assertNotNull(token); | ||
|
||
for (Cookie cookie : driver.manage().getCookies()) { | ||
driver.manage().deleteCookie(cookie); | ||
} | ||
|
||
oauth.openLoginForm(); | ||
|
||
oauth.realm(REALM_NAME); | ||
oauth.clientId(DEVICE_APP_PUBLIC_CUSTOM_CONSENT); | ||
|
||
oauth.fillLoginForm("device-login", "password"); | ||
|
||
for (Cookie cookie : driver.manage().getCookies()) { | ||
driver.manage().deleteCookie(cookie); | ||
} | ||
|
||
oauth.openLoginForm(); | ||
|
||
response = oauth.doDeviceAuthorizationRequest(DEVICE_APP_PUBLIC_CUSTOM_CONSENT, null); | ||
|
||
openVerificationPage(response.getVerificationUriComplete()); | ||
|
||
// Consent | ||
Assert.assertTrue(grantPage.getDisplayedGrants().contains("This is the custom consent screen text.")); | ||
grantPage.accept(); | ||
|
||
// Token request from device | ||
tokenResponse = oauth.doDeviceTokenRequest(DEVICE_APP_PUBLIC, null, response.getDeviceCode()); | ||
|
||
assertEquals(400, tokenResponse.getStatusCode()); | ||
assertEquals("unauthorized client", tokenResponse.getErrorDescription()); | ||
} | ||
|
||
@Test | ||
public void testPublicClientOptionalScope() throws Exception { | ||
// Device Authorization Request from device - check giving optional scope phone | ||
|