blob: d24cad1e4b951ab711bcc620c917978ebfbd6b1d [file] [log] [blame]
Michael Hanle25dea22015-09-24 19:37:56 +02001package de.ids_mannheim.korap.web.service;
2
3import de.ids_mannheim.korap.config.AuthCodeInfo;
Michael Hanldaf86602016-05-12 14:31:52 +02004import de.ids_mannheim.korap.config.BeanConfigTest;
Michael Hanle25dea22015-09-24 19:37:56 +02005import de.ids_mannheim.korap.config.ClientInfo;
Michael Hanldaf86602016-05-12 14:31:52 +02006import de.ids_mannheim.korap.config.ContextHolder;
Michael Hanle25dea22015-09-24 19:37:56 +02007import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanl482f30d2015-09-25 12:39:46 +02008import de.ids_mannheim.korap.handlers.OAuth2Handler;
Michael Hanle25dea22015-09-24 19:37:56 +02009import de.ids_mannheim.korap.interfaces.EncryptionIface;
Michael Hanldaf86602016-05-12 14:31:52 +020010import de.ids_mannheim.korap.interfaces.db.PersistenceClient;
Michael Hanl482f30d2015-09-25 12:39:46 +020011import de.ids_mannheim.korap.user.TokenContext;
Michael Hanle25dea22015-09-24 19:37:56 +020012import org.junit.Assert;
13import org.junit.BeforeClass;
14import org.junit.Test;
15
Michael Hanlcedf7212016-05-28 10:43:09 +020016import static org.junit.Assert.assertNotNull;
Michael Hanl33829ec2016-05-28 17:03:38 +020017import static org.junit.Assert.assertNull;
Michael Hanlcedf7212016-05-28 10:43:09 +020018
Michael Hanle25dea22015-09-24 19:37:56 +020019/**
20 * @author hanl
21 * @date 13/05/2015
22 */
23
Michael Hanldaf86602016-05-12 14:31:52 +020024public class OAuth2HandlerTest extends BeanConfigTest {
Michael Hanle25dea22015-09-24 19:37:56 +020025
26 private static ClientInfo info;
Michael Hanldaf86602016-05-12 14:31:52 +020027
Michael Hanl482f30d2015-09-25 12:39:46 +020028 private static final String SCOPES = "search preferences queries account";
Michael Hanle25dea22015-09-24 19:37:56 +020029
Michael Hanl8abaf9e2016-05-23 16:46:35 +020030
Michael Hanle25dea22015-09-24 19:37:56 +020031 @BeforeClass
Michael Hanl8abaf9e2016-05-23 16:46:35 +020032 public static void setup () throws Exception {
Michael Hanldaf86602016-05-12 14:31:52 +020033 // BeanConfiguration.loadClasspathContext("default-config.xml");
34 // handler = new OAuth2Handler(
35 // BeanConfiguration.getKustvaktContext().getPersistenceClient());
36 // crypto = BeanConfiguration.getKustvaktContext().getEncryption();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020037 // info = new ClientInfo(crypto.createID(), crypto.createToken());
Michael Hanldaf86602016-05-12 14:31:52 +020038 // info.setConfidential(true);
39 // //todo: support for subdomains?!
40 // info.setUrl("http://localhost:8080/api/v0.1");
41 // info.setRedirect_uri("testwebsite/login");
42 //
43 // helper = TestHelper.newInstance();
44 // helper.setupAccount();
45 // PersistenceClient cl = helper.getBean(ContextHolder.KUSTVAKT_DB);
46 // handler = new OAuth2Handler(cl);
47 // handler.registerClient(info, helper.getUser());
48 // crypto = helper.getBean(ContextHolder.KUSTVAKT_ENCRYPTION);
Michael Hanle25dea22015-09-24 19:37:56 +020049 }
50
Michael Hanl8abaf9e2016-05-23 16:46:35 +020051
Michael Hanle25dea22015-09-24 19:37:56 +020052 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020053 public void testStoreAuthorizationCodeThrowsNoException ()
Michael Hanle25dea22015-09-24 19:37:56 +020054 throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +020055 String auth_code = helper().getContext().getEncryption().createToken();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020056 AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(), auth_code);
Michael Hanl482f30d2015-09-25 12:39:46 +020057 codeInfo.setScopes(SCOPES);
Michael Hanle25dea22015-09-24 19:37:56 +020058
Michael Hanl8abaf9e2016-05-23 16:46:35 +020059 OAuth2Handler handler = new OAuth2Handler(helper().getContext()
60 .getPersistenceClient());
Michael Hanldaf86602016-05-12 14:31:52 +020061 handler.authorize(codeInfo, helper().getUser());
Michael Hanle25dea22015-09-24 19:37:56 +020062 codeInfo = handler.getAuthorization(auth_code);
Michael Hanlcedf7212016-05-28 10:43:09 +020063 assertNotNull("client is null!", codeInfo);
Michael Hanle25dea22015-09-24 19:37:56 +020064 }
65
Michael Hanl8abaf9e2016-05-23 16:46:35 +020066
Michael Hanle25dea22015-09-24 19:37:56 +020067 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020068 public void testAuthorizationCodeRemoveThrowsNoException ()
Michael Hanle25dea22015-09-24 19:37:56 +020069 throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +020070 String auth_code = helper().getContext().getEncryption().createToken();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020071 AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(), auth_code);
Michael Hanl482f30d2015-09-25 12:39:46 +020072 codeInfo.setScopes(SCOPES);
Michael Hanle25dea22015-09-24 19:37:56 +020073
Michael Hanl8abaf9e2016-05-23 16:46:35 +020074 OAuth2Handler handler = new OAuth2Handler(helper().getContext()
75 .getPersistenceClient());
Michael Hanldaf86602016-05-12 14:31:52 +020076 handler.authorize(codeInfo, helper().getUser());
77 String t = helper().getContext().getEncryption().createToken();
78 String refresh = helper().getContext().getEncryption().createToken();
Michael Hanl482f30d2015-09-25 12:39:46 +020079 handler.addToken(codeInfo.getCode(), t, refresh, 7200);
Michael Hanle25dea22015-09-24 19:37:56 +020080
81 TokenContext ctx = handler.getContext(t);
Michael Hanlcedf7212016-05-28 10:43:09 +020082 assertNotNull("context is null", ctx);
Michael Hanle25dea22015-09-24 19:37:56 +020083
84 AuthCodeInfo c2 = handler.getAuthorization(codeInfo.getCode());
Michael Hanl33829ec2016-05-28 17:03:38 +020085 assertNull("clearing authorization failed", c2);
Michael Hanle25dea22015-09-24 19:37:56 +020086 }
87
Michael Hanl8abaf9e2016-05-23 16:46:35 +020088
Michael Hanle25dea22015-09-24 19:37:56 +020089 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020090 public void testTokenEndpointRedirect () {
Michael Hanl482f30d2015-09-25 12:39:46 +020091
92 }
93
Michael Hanl8abaf9e2016-05-23 16:46:35 +020094
Michael Hanl482f30d2015-09-25 12:39:46 +020095 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020096 public void testStoreAccessCodeViaAuthCodeThrowsNoException () {
Michael Hanldaf86602016-05-12 14:31:52 +020097 String auth_code = helper().getContext().getEncryption().createToken();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020098 AuthCodeInfo codeInfo = new AuthCodeInfo(info.getClient_id(), auth_code);
Michael Hanl482f30d2015-09-25 12:39:46 +020099 codeInfo.setScopes(SCOPES);
Michael Hanle25dea22015-09-24 19:37:56 +0200100
101 }
102
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200103
Michael Hanle25dea22015-09-24 19:37:56 +0200104 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200105 public void testDeleteAccessCodesByUserDeleteCascade () {
Michael Hanle25dea22015-09-24 19:37:56 +0200106
107 }
108
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200109
Michael Hanle25dea22015-09-24 19:37:56 +0200110 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200111 public void testAccessTokenbyUserDeleteCascade () {
Michael Hanle25dea22015-09-24 19:37:56 +0200112
113 }
Michael Hanl482f30d2015-09-25 12:39:46 +0200114
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200115
Michael Hanl482f30d2015-09-25 12:39:46 +0200116 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200117 public void testRefreshToken () {
Michael Hanl482f30d2015-09-25 12:39:46 +0200118
119 }
120
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200121
Michael Hanl482f30d2015-09-25 12:39:46 +0200122 // fixme: exception thrown?!
123 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200124 public void testAccessTokenExpired () {
Michael Hanl482f30d2015-09-25 12:39:46 +0200125
126 }
Michael Hanldaf86602016-05-12 14:31:52 +0200127
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200128
Michael Hanldaf86602016-05-12 14:31:52 +0200129 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200130 public void initMethod () throws KustvaktException {
Michael Hanldaf86602016-05-12 14:31:52 +0200131 helper().setupAccount();
132
133 EncryptionIface crypto = helper().getContext().getEncryption();
134 info = new ClientInfo(crypto.createID(), crypto.createToken());
135 info.setConfidential(true);
136 //todo: support for subdomains?!
137 info.setUrl("http://localhost:8080/api/v0.1");
138 info.setRedirect_uri("testwebsite/login");
139 PersistenceClient cl = helper().getBean(ContextHolder.KUSTVAKT_DB);
140 OAuth2Handler handler = new OAuth2Handler(cl);
141 handler.registerClient(info, helper().getUser());
142 }
Michael Hanle25dea22015-09-24 19:37:56 +0200143}