Search in sources :

Example 1 with OAuthApiConnection

use of org.wikidata.wdtk.wikibaseapi.OAuthApiConnection in project OpenRefine by OpenRefine.

the class LoginCommandTest method testOwnerOnlyConsumerLoginRememberCredentials.

@Test
public void testOwnerOnlyConsumerLoginRememberCredentials() throws Exception {
    when(request.getParameter("csrf_token")).thenReturn(Command.csrfFactory.getFreshToken());
    when(request.getParameter("remember-credentials")).thenReturn("on");
    when(request.getParameter(API_ENDPOINT)).thenReturn(apiEndpoint);
    when(request.getParameter(CONSUMER_TOKEN)).thenReturn(consumerToken);
    when(request.getParameter(CONSUMER_SECRET)).thenReturn(consumerSecret);
    when(request.getParameter(ACCESS_TOKEN)).thenReturn(accessToken);
    when(request.getParameter(ACCESS_SECRET)).thenReturn(accessSecret);
    when(request.getCookies()).thenReturn(makeRequestCookies());
    when(connectionManager.login(apiEndpoint, consumerToken, consumerSecret, accessToken, accessSecret)).thenReturn(true);
    when(connectionManager.isLoggedIn(apiEndpoint)).thenReturn(true);
    when(connectionManager.getUsername(apiEndpoint)).thenReturn(username);
    OAuthApiConnection connection = mock(OAuthApiConnection.class);
    when(connectionManager.getConnection(apiEndpoint)).thenReturn(connection);
    when(connection.getCurrentUser()).thenReturn(username);
    command.doPost(request, response);
    verify(connectionManager, times(1)).login(apiEndpoint, consumerToken, consumerSecret, accessToken, accessSecret);
    assertLogin();
    Map<String, Cookie> cookies = getCookieMap(cookieCaptor.getAllValues());
    // If logging in with owner-only consumer,
    // cookies for the username/password login should be cleared.
    cookieMap.forEach((key, value) -> assertCookieEquals(cookies.get(apiEndpointPrefix + WIKIBASE_COOKIE_PREFIX + key), "", 0));
    assertCookieEquals(cookies.get(apiEndpointPrefix + USERNAME), "", 0);
    assertCookieEquals(cookies.get(apiEndpointPrefix + CONSUMER_TOKEN), consumerToken, ONE_YEAR);
    assertCookieEquals(cookies.get(apiEndpointPrefix + CONSUMER_SECRET), consumerSecret, ONE_YEAR);
    assertCookieEquals(cookies.get(apiEndpointPrefix + ACCESS_TOKEN), accessToken, ONE_YEAR);
    assertCookieEquals(cookies.get(apiEndpointPrefix + ACCESS_SECRET), accessSecret, ONE_YEAR);
}
Also used : HttpCookie(java.net.HttpCookie) Cookie(javax.servlet.http.Cookie) OAuthApiConnection(org.wikidata.wdtk.wikibaseapi.OAuthApiConnection) Test(org.testng.annotations.Test)

Example 2 with OAuthApiConnection

use of org.wikidata.wdtk.wikibaseapi.OAuthApiConnection in project OpenRefine by OpenRefine.

the class LoginCommandTest method testCookieDecoding.

@Test
public void testCookieDecoding() throws Exception {
    when(request.getParameter("csrf_token")).thenReturn(Command.csrfFactory.getFreshToken());
    when(request.getParameter(API_ENDPOINT)).thenReturn(apiEndpoint);
    Cookie consumerTokenCookie = new Cookie(apiEndpointPrefix + CONSUMER_TOKEN, "malformed+consumer+token+%0D%0A+%25%3F");
    Cookie consumerSecretCookie = new Cookie(apiEndpointPrefix + CONSUMER_SECRET, consumerSecret);
    Cookie accessTokenCookie = new Cookie(apiEndpointPrefix + ACCESS_TOKEN, accessToken);
    Cookie accessSecretCookie = new Cookie(apiEndpointPrefix + ACCESS_SECRET, accessSecret);
    when(request.getCookies()).thenReturn(new Cookie[] { consumerTokenCookie, consumerSecretCookie, accessTokenCookie, accessSecretCookie });
    when(connectionManager.login(apiEndpoint, "malformed consumer token \r\n %?", consumerSecret, accessToken, accessSecret)).thenReturn(true);
    when(connectionManager.isLoggedIn(apiEndpoint)).thenReturn(true);
    when(connectionManager.getUsername(apiEndpoint)).thenReturn(username);
    OAuthApiConnection connection = mock(OAuthApiConnection.class);
    when(connectionManager.getConnection(apiEndpoint)).thenReturn(connection);
    when(connection.getCurrentUser()).thenReturn(username);
    command.doPost(request, response);
    verify(connectionManager).login(apiEndpoint, "malformed consumer token \r\n %?", consumerSecret, accessToken, accessSecret);
}
Also used : HttpCookie(java.net.HttpCookie) Cookie(javax.servlet.http.Cookie) OAuthApiConnection(org.wikidata.wdtk.wikibaseapi.OAuthApiConnection) Test(org.testng.annotations.Test)

Example 3 with OAuthApiConnection

use of org.wikidata.wdtk.wikibaseapi.OAuthApiConnection in project OpenRefine by OpenRefine.

the class LoginCommandTest method testOwnerOnlyConsumerLogin.

@Test
public void testOwnerOnlyConsumerLogin() throws Exception {
    when(request.getParameter("csrf_token")).thenReturn(Command.csrfFactory.getFreshToken());
    when(request.getParameter(API_ENDPOINT)).thenReturn(apiEndpoint);
    when(request.getParameter(CONSUMER_TOKEN)).thenReturn(consumerToken);
    when(request.getParameter(CONSUMER_SECRET)).thenReturn(consumerSecret);
    when(request.getParameter(ACCESS_TOKEN)).thenReturn(accessToken);
    when(request.getParameter(ACCESS_SECRET)).thenReturn(accessSecret);
    when(connectionManager.login(apiEndpoint, consumerToken, consumerSecret, accessToken, accessSecret)).thenReturn(true);
    when(connectionManager.isLoggedIn(apiEndpoint)).thenReturn(true);
    when(connectionManager.getUsername(apiEndpoint)).thenReturn(username);
    OAuthApiConnection connection = mock(OAuthApiConnection.class);
    when(connectionManager.getConnection(apiEndpoint)).thenReturn(connection);
    when(connection.getCurrentUser()).thenReturn(username);
    command.doPost(request, response);
    verify(connectionManager, times(1)).login(apiEndpoint, consumerToken, consumerSecret, accessToken, accessSecret);
    assertLogin();
    Map<String, Cookie> cookies = getCookieMap(cookieCaptor.getAllValues());
    assertEquals(cookies.size(), 5);
    assertCookieEquals(cookies.get(apiEndpointPrefix + USERNAME), "", 0);
    assertCookieEquals(cookies.get(apiEndpointPrefix + CONSUMER_TOKEN), "", 0);
    assertCookieEquals(cookies.get(apiEndpointPrefix + CONSUMER_SECRET), "", 0);
    assertCookieEquals(cookies.get(apiEndpointPrefix + ACCESS_TOKEN), "", 0);
    assertCookieEquals(cookies.get(apiEndpointPrefix + ACCESS_SECRET), "", 0);
}
Also used : HttpCookie(java.net.HttpCookie) Cookie(javax.servlet.http.Cookie) OAuthApiConnection(org.wikidata.wdtk.wikibaseapi.OAuthApiConnection) Test(org.testng.annotations.Test)

Example 4 with OAuthApiConnection

use of org.wikidata.wdtk.wikibaseapi.OAuthApiConnection in project OpenRefine by OpenRefine.

the class LoginCommandTest method testOwnerOnlyConsumerLoginWithCookies.

@Test
public void testOwnerOnlyConsumerLoginWithCookies() throws Exception {
    when(request.getParameter("csrf_token")).thenReturn(Command.csrfFactory.getFreshToken());
    when(request.getParameter(API_ENDPOINT)).thenReturn(apiEndpoint);
    Cookie consumerTokenCookie = new Cookie(apiEndpointPrefix + CONSUMER_TOKEN, consumerToken);
    Cookie consumerSecretCookie = new Cookie(apiEndpointPrefix + CONSUMER_SECRET, consumerSecret);
    Cookie accessTokenCookie = new Cookie(apiEndpointPrefix + ACCESS_TOKEN, accessToken);
    Cookie accessSecretCookie = new Cookie(apiEndpointPrefix + ACCESS_SECRET, accessSecret);
    when(request.getCookies()).thenReturn(new Cookie[] { consumerTokenCookie, consumerSecretCookie, accessTokenCookie, accessSecretCookie });
    when(connectionManager.login(apiEndpoint, consumerToken, consumerSecret, accessToken, accessSecret)).thenReturn(true);
    when(connectionManager.isLoggedIn(apiEndpoint)).thenReturn(true);
    when(connectionManager.getUsername(apiEndpoint)).thenReturn(username);
    OAuthApiConnection connection = mock(OAuthApiConnection.class);
    when(connectionManager.getConnection(apiEndpoint)).thenReturn(connection);
    when(connection.getCurrentUser()).thenReturn(username);
    command.doPost(request, response);
    verify(connectionManager, times(1)).login(apiEndpoint, consumerToken, consumerSecret, accessToken, accessSecret);
    assertLogin();
    Map<String, Cookie> cookies = getCookieMap(cookieCaptor.getAllValues());
    assertEquals(cookies.size(), 5);
    assertCookieEquals(cookies.get(apiEndpointPrefix + USERNAME), "", 0);
    assertCookieEquals(cookies.get(apiEndpointPrefix + CONSUMER_TOKEN), consumerToken, ONE_YEAR);
    assertCookieEquals(cookies.get(apiEndpointPrefix + CONSUMER_SECRET), consumerSecret, ONE_YEAR);
    assertCookieEquals(cookies.get(apiEndpointPrefix + ACCESS_TOKEN), accessToken, ONE_YEAR);
    assertCookieEquals(cookies.get(apiEndpointPrefix + ACCESS_SECRET), accessSecret, ONE_YEAR);
}
Also used : HttpCookie(java.net.HttpCookie) Cookie(javax.servlet.http.Cookie) OAuthApiConnection(org.wikidata.wdtk.wikibaseapi.OAuthApiConnection) Test(org.testng.annotations.Test)

Example 5 with OAuthApiConnection

use of org.wikidata.wdtk.wikibaseapi.OAuthApiConnection in project OpenRefine by OpenRefine.

the class LoginCommandTest method testCookieEncoding.

@Test
public void testCookieEncoding() throws Exception {
    when(request.getParameter("csrf_token")).thenReturn(Command.csrfFactory.getFreshToken());
    when(request.getParameter("remember-credentials")).thenReturn("on");
    when(request.getParameter(API_ENDPOINT)).thenReturn(apiEndpoint);
    when(request.getParameter(CONSUMER_TOKEN)).thenReturn("malformed consumer token \r\n %?");
    when(request.getParameter(CONSUMER_SECRET)).thenReturn(consumerSecret);
    when(request.getParameter(ACCESS_TOKEN)).thenReturn(accessToken);
    when(request.getParameter(ACCESS_SECRET)).thenReturn(accessSecret);
    when(request.getCookies()).thenReturn(makeRequestCookies());
    when(connectionManager.login(apiEndpoint, "malformed consumer token \r\n %?", consumerSecret, accessToken, accessSecret)).thenReturn(true);
    when(connectionManager.isLoggedIn(apiEndpoint)).thenReturn(true);
    when(connectionManager.getUsername(apiEndpoint)).thenReturn(username);
    OAuthApiConnection connection = mock(OAuthApiConnection.class);
    when(connectionManager.getConnection(apiEndpoint)).thenReturn(connection);
    when(connection.getCurrentUser()).thenReturn(username);
    command.doPost(request, response);
    Map<String, Cookie> cookies = getCookieMap(cookieCaptor.getAllValues());
    assertNotEquals(cookies.get(apiEndpointPrefix + CONSUMER_TOKEN).getValue(), "malformed consumer token \r\n %?");
    assertEquals(cookies.get(apiEndpointPrefix + CONSUMER_TOKEN).getValue(), "malformed+consumer+token+%0D%0A+%25%3F");
}
Also used : HttpCookie(java.net.HttpCookie) Cookie(javax.servlet.http.Cookie) OAuthApiConnection(org.wikidata.wdtk.wikibaseapi.OAuthApiConnection) Test(org.testng.annotations.Test)

Aggregations

HttpCookie (java.net.HttpCookie)5 Cookie (javax.servlet.http.Cookie)5 Test (org.testng.annotations.Test)5 OAuthApiConnection (org.wikidata.wdtk.wikibaseapi.OAuthApiConnection)5