blob: a8742fa1c1f152c02eaa1e21cc7e694347963549 [file] [log] [blame]
margaretha541b8cc2018-01-10 13:02:46 +01001package de.ids_mannheim.korap.web;
Michael Hanle25dea22015-09-24 19:37:56 +02002
margarethaf68daa62017-09-21 02:11:24 +02003import static org.junit.Assert.assertNotNull;
4import static org.junit.Assert.assertNull;
5import static org.junit.Assert.assertTrue;
6
margaretha843c4632017-10-25 19:16:12 +02007import org.junit.Ignore;
margarethaf68daa62017-09-21 02:11:24 +02008import org.junit.Test;
9
Michael Hanle25dea22015-09-24 19:37:56 +020010import de.ids_mannheim.korap.config.AuthCodeInfo;
Michael Hanldaf86602016-05-12 14:31:52 +020011import de.ids_mannheim.korap.config.BeanConfigTest;
Michael Hanle25dea22015-09-24 19:37:56 +020012import de.ids_mannheim.korap.config.ClientInfo;
Michael Hanldaf86602016-05-12 14:31:52 +020013import de.ids_mannheim.korap.config.ContextHolder;
Michael Hanle25dea22015-09-24 19:37:56 +020014import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanl482f30d2015-09-25 12:39:46 +020015import de.ids_mannheim.korap.handlers.OAuth2Handler;
Michael Hanle25dea22015-09-24 19:37:56 +020016import de.ids_mannheim.korap.interfaces.EncryptionIface;
Michael Hanldaf86602016-05-12 14:31:52 +020017import de.ids_mannheim.korap.interfaces.db.PersistenceClient;
margaretha0e8f4e72018-04-05 14:11:52 +020018import de.ids_mannheim.korap.security.context.TokenContext;
Michael Hanlcedf7212016-05-28 10:43:09 +020019
Michael Hanle25dea22015-09-24 19:37:56 +020020/**
margaretha843c4632017-10-25 19:16:12 +020021 * EM: To do: not implemented in the new DB yet
Michael Hanle25dea22015-09-24 19:37:56 +020022 * @author hanl
23 * @date 13/05/2015
24 */
margaretha843c4632017-10-25 19:16:12 +020025@Ignore
Michael Hanldaf86602016-05-12 14:31:52 +020026public class OAuth2HandlerTest extends BeanConfigTest {
Michael Hanle25dea22015-09-24 19:37:56 +020027
28 private static ClientInfo info;
Michael Hanldaf86602016-05-12 14:31:52 +020029
Michael Hanl482f30d2015-09-25 12:39:46 +020030 private static final String SCOPES = "search preferences queries account";
Michael Hanle25dea22015-09-24 19:37:56 +020031
Michael Hanl8abaf9e2016-05-23 16:46:35 +020032
Michael Hanle25dea22015-09-24 19:37:56 +020033 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020034 public void testStoreAuthorizationCodeThrowsNoException ()
Michael Hanle25dea22015-09-24 19:37:56 +020035 throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +020036 String auth_code = helper().getContext().getEncryption().createToken();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020037 AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(), auth_code);
Michael Hanl482f30d2015-09-25 12:39:46 +020038 codeInfo.setScopes(SCOPES);
Michael Hanle25dea22015-09-24 19:37:56 +020039
Michael Hanl8abaf9e2016-05-23 16:46:35 +020040 OAuth2Handler handler = new OAuth2Handler(helper().getContext()
41 .getPersistenceClient());
Michael Hanldaf86602016-05-12 14:31:52 +020042 handler.authorize(codeInfo, helper().getUser());
Michael Hanlc0ed00f2016-06-23 14:33:10 +020043 assertTrue("couldn't find entry in cache", handler.hasCacheEntry(codeInfo.getCode()));
Michael Hanle25dea22015-09-24 19:37:56 +020044 codeInfo = handler.getAuthorization(auth_code);
Michael Hanlcedf7212016-05-28 10:43:09 +020045 assertNotNull("client is null!", codeInfo);
Michael Hanle25dea22015-09-24 19:37:56 +020046 }
47
Michael Hanl8abaf9e2016-05-23 16:46:35 +020048
Michael Hanle25dea22015-09-24 19:37:56 +020049 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020050 public void testAuthorizationCodeRemoveThrowsNoException ()
Michael Hanle25dea22015-09-24 19:37:56 +020051 throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +020052 String auth_code = helper().getContext().getEncryption().createToken();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020053 AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(), auth_code);
Michael Hanl482f30d2015-09-25 12:39:46 +020054 codeInfo.setScopes(SCOPES);
Michael Hanle25dea22015-09-24 19:37:56 +020055
Michael Hanl8abaf9e2016-05-23 16:46:35 +020056 OAuth2Handler handler = new OAuth2Handler(helper().getContext()
57 .getPersistenceClient());
Michael Hanldaf86602016-05-12 14:31:52 +020058 handler.authorize(codeInfo, helper().getUser());
59 String t = helper().getContext().getEncryption().createToken();
60 String refresh = helper().getContext().getEncryption().createToken();
Michael Hanl482f30d2015-09-25 12:39:46 +020061 handler.addToken(codeInfo.getCode(), t, refresh, 7200);
Michael Hanle25dea22015-09-24 19:37:56 +020062
Michael Hanlc0ed00f2016-06-23 14:33:10 +020063 TokenContext ctx = handler.getPersistenceHandler().getContext(t);
Michael Hanlcedf7212016-05-28 10:43:09 +020064 assertNotNull("context is null", ctx);
Michael Hanle25dea22015-09-24 19:37:56 +020065
66 AuthCodeInfo c2 = handler.getAuthorization(codeInfo.getCode());
Michael Hanl33829ec2016-05-28 17:03:38 +020067 assertNull("clearing authorization failed", c2);
Michael Hanle25dea22015-09-24 19:37:56 +020068 }
69
Michael Hanl8abaf9e2016-05-23 16:46:35 +020070
Michael Hanle25dea22015-09-24 19:37:56 +020071 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020072 public void testTokenEndpointRedirect () {
Michael Hanl482f30d2015-09-25 12:39:46 +020073
74 }
75
Michael Hanl8abaf9e2016-05-23 16:46:35 +020076
Michael Hanl482f30d2015-09-25 12:39:46 +020077 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020078 public void testStoreAccessCodeViaAuthCodeThrowsNoException () {
Michael Hanldaf86602016-05-12 14:31:52 +020079 String auth_code = helper().getContext().getEncryption().createToken();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020080 AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(), auth_code);
Michael Hanl482f30d2015-09-25 12:39:46 +020081 codeInfo.setScopes(SCOPES);
Michael Hanle25dea22015-09-24 19:37:56 +020082
83 }
84
Michael Hanl8abaf9e2016-05-23 16:46:35 +020085
Michael Hanle25dea22015-09-24 19:37:56 +020086 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020087 public void testDeleteAccessCodesByUserDeleteCascade () {
Michael Hanle25dea22015-09-24 19:37:56 +020088
89 }
90
Michael Hanl8abaf9e2016-05-23 16:46:35 +020091
Michael Hanle25dea22015-09-24 19:37:56 +020092 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020093 public void testAccessTokenbyUserDeleteCascade () {
Michael Hanle25dea22015-09-24 19:37:56 +020094
95 }
Michael Hanl482f30d2015-09-25 12:39:46 +020096
Michael Hanl8abaf9e2016-05-23 16:46:35 +020097
Michael Hanl482f30d2015-09-25 12:39:46 +020098 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020099 public void testRefreshToken () {
Michael Hanl482f30d2015-09-25 12:39:46 +0200100
101 }
102
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200103
Michael Hanl482f30d2015-09-25 12:39:46 +0200104 // fixme: exception thrown?!
105 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200106 public void testAccessTokenExpired () {
Michael Hanl482f30d2015-09-25 12:39:46 +0200107
108 }
Michael Hanldaf86602016-05-12 14:31:52 +0200109
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200110
Michael Hanldaf86602016-05-12 14:31:52 +0200111 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200112 public void initMethod () throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +0200113 helper().setupAccount();
114
115 EncryptionIface crypto = helper().getContext().getEncryption();
Michael Hanld8aa6212016-06-03 12:48:43 +0200116 info = new ClientInfo(crypto.createRandomNumber(), crypto.createToken());
Michael Hanldaf86602016-05-12 14:31:52 +0200117 info.setConfidential(true);
118 //todo: support for subdomains?!
119 info.setUrl("http://localhost:8080/api/v0.1");
120 info.setRedirect_uri("testwebsite/login");
121 PersistenceClient cl = helper().getBean(ContextHolder.KUSTVAKT_DB);
122 OAuth2Handler handler = new OAuth2Handler(cl);
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200123 handler.getPersistenceHandler().registerClient(info, helper().getUser());
Michael Hanldaf86602016-05-12 14:31:52 +0200124 }
Michael Hanle25dea22015-09-24 19:37:56 +0200125}