Skip to content

Commit 699cf88

Browse files
author
Joerg Huber
committed
Use new SecurityServiceFactory rather than BearerSecurityFactory.
1 parent e348665 commit 699cf88

10 files changed

Lines changed: 447 additions & 181 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* BaseSecurityOperations.java
3+
* Created: 14 Jul 2015
4+
*
5+
* Copyright 2015 Systemic Pty Ltd
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8+
* in compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed under the License
13+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14+
* or implied. See the License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
package sif3.common.test.security;
19+
20+
import java.nio.charset.Charset;
21+
import java.text.ParseException;
22+
import java.util.Date;
23+
24+
import org.apache.commons.codec.binary.Base64;
25+
26+
import sif3.common.model.EnvironmentKey;
27+
import sif3.common.model.security.TokenCoreInfo;
28+
import sif3.common.model.security.TokenInfo;
29+
import au.com.systemic.framework.utils.DateUtils;
30+
31+
/**
32+
* This is just a dummy implementation on how a token could be generated. It uses the applicationKey and a timestamp.
33+
*
34+
* @author Joerg Huber
35+
*
36+
*/
37+
public class BaseSecurityOperations
38+
{
39+
private static final int MINUTES = 60;
40+
41+
@SuppressWarnings("unused")
42+
public static TokenInfo createToken(TokenCoreInfo coreInfo, String password)
43+
{
44+
String iso8601Str = "2016-05-26T14:01:00Z";
45+
String token = coreInfo.getAppUserInfo().getApplicationKey()+":"+iso8601Str;
46+
token = new String(Base64.encodeBase64(token.getBytes()), Charset.forName("ASCII"));
47+
TokenInfo newToken = null;
48+
49+
try
50+
{
51+
newToken = new TokenInfo(token, DateUtils.stringToDate(iso8601Str, DateUtils.ISO_8601));
52+
}
53+
catch (Exception ex)
54+
{
55+
System.out.println("Failed to create token");
56+
ex.printStackTrace();
57+
return null;
58+
}
59+
/*
60+
TokenInfo newToken = new TokenInfo(coreInfo);
61+
Date expiryDate = DateUtils.getDateWithAddedMinutes(new Date(), MINUTES);
62+
String token = newToken.getAppUserInfo().getApplicationKey()+":"+DateUtils.getISO8601(expiryDate);
63+
newToken.setTokenExpiryDate(expiryDate);
64+
newToken.setToken(new String(Base64.encodeBase64(token.getBytes()), Charset.forName("ASCII")));
65+
*/
66+
return newToken;
67+
}
68+
69+
public static TokenInfo getTokenInfo(String token)
70+
{
71+
TokenInfo newToken = new TokenInfo(token);
72+
String decodedToken = new String(Base64.decodeBase64(token), Charset.forName("ASCII"));
73+
System.out.println("Decoded Token: "+decodedToken);
74+
int pos = decodedToken.indexOf(":");
75+
String appKey = decodedToken.substring(0, pos);
76+
String expDate = decodedToken.substring(pos+1);
77+
// newToken.setAppUserInfo(new EnvironmentKey(null, appKey));
78+
newToken.setAppUserInfo(new EnvironmentKey("test", appKey));
79+
try
80+
{
81+
newToken.setTokenExpiryDate(DateUtils.stringToDate(expDate, DateUtils.ISO_8601));
82+
}
83+
catch (ParseException e)
84+
{
85+
System.out.println("Failed to convert date string to date. Expected date format is "+DateUtils.ISO_8601.toString()+". Date String is "+expDate);
86+
return null;
87+
}
88+
89+
return newToken;
90+
}
91+
92+
public static void main(String[] args)
93+
{
94+
TokenCoreInfo coreInfo = new TokenCoreInfo();
95+
coreInfo.setAppUserInfo(new EnvironmentKey(null, "BearerTest"));
96+
TokenInfo tokenInfo = createToken(coreInfo, "test");
97+
System.out.println("Created Token: "+tokenInfo);
98+
99+
System.out.println("Extracted token info: "+getTokenInfo(tokenInfo.getToken()));
100+
}
101+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* DummySecurityService.java
3+
* Created: 15/12/2014
4+
*
5+
* Copyright 2014 Systemic Pty Ltd
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software distributed under the License
14+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15+
* or implied.
16+
* See the License for the specific language governing permissions and limitations under the License.
17+
*/
18+
package sif3.common.test.security;
19+
20+
import sif3.common.model.RequestMetadata;
21+
import sif3.common.model.security.TokenCoreInfo;
22+
import sif3.common.model.security.TokenInfo;
23+
import sif3.common.security.AbstractSecurityService;
24+
import au.com.systemic.framework.utils.AdvancedProperties;
25+
26+
public class DummySecurityService extends AbstractSecurityService
27+
{
28+
// private static final long MINUTE = 1000*60;
29+
30+
public DummySecurityService(AdvancedProperties properties)
31+
{
32+
super(properties);
33+
System.out.println("DummySecurityService Constructor called with property: " + getServiceProperties());
34+
}
35+
36+
@Override
37+
public TokenInfo getTokenInfo(String securityToken, RequestMetadata requestMetadata)
38+
{
39+
System.out.println("DummySecurityService.getTokenInfo() called for securityToken = "+securityToken+" and request metadata = "+requestMetadata);
40+
TokenInfo tokenInfo = BaseSecurityOperations.getTokenInfo(securityToken);
41+
42+
// long expireTime = MINUTE*5; // expire every x minute.
43+
// TokenInfo tokenInfo = new TokenInfo(securityToken);
44+
// tokenInfo.setTokenExpiryDate(new Date(((new Date()).getTime()) + expireTime));
45+
// tokenInfo.setSessionToken("80404112-96f7-4b2e-a118-3db458608877");
46+
// tokenInfo.setAppUserInfo(new EnvironmentKey(null, "BearerTest"));
47+
// tokenInfo.setAppUserInfo(new EnvironmentKey(null, "BearerTest1"));
48+
return tokenInfo;
49+
}
50+
51+
@Override
52+
public boolean validateToken(String securityToken, RequestMetadata requestMetadata)
53+
{
54+
System.out.println("DummySecurityService.validateToken() called for securityToken = "+securityToken+" and request metadata = "+requestMetadata);
55+
return true;
56+
}
57+
58+
@Override
59+
public TokenInfo generateToken(TokenCoreInfo coreInfo, String password)
60+
{
61+
System.out.println("TokenCoreInfo: "+coreInfo);
62+
System.out.println("Value for prop2: "+coreInfo.getOtherInfo().get("prop2"));
63+
return BaseSecurityOperations.createToken(coreInfo, getServiceProperties().getPropertyAsString("env.pwd", null));
64+
}
65+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* TestSecurityServiceFactory.java Created: 15/12/2014
3+
*
4+
* Copyright 2014 Systemic Pty Ltd
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7+
* in compliance with the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software distributed under the License
12+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
* or implied. See the License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package sif3.common.test.security;
17+
18+
import au.com.systemic.framework.utils.AdvancedProperties;
19+
import au.com.systemic.framework.utils.PropertyManager;
20+
import sif3.common.CommonConstants.AdapterType;
21+
import sif3.common.model.EnvironmentKey;
22+
import sif3.common.model.security.TokenCoreInfo;
23+
import sif3.common.model.security.TokenInfo;
24+
import sif3.common.persist.common.HibernateUtil;
25+
import sif3.common.persist.model.ExternalSecurityService;
26+
import sif3.common.security.AbstractSecurityService;
27+
import sif3.common.security.SecurityServiceFactory;
28+
29+
public class TestSecurityServiceFactory
30+
{
31+
private static final boolean IS_PROVDER = false;
32+
33+
private static final String PROV_PROP_FILE_NAME = "StudentProvider";
34+
private static final String CONS_PROP_FILE_NAME = "StudentConsumer";
35+
36+
private AdvancedProperties properties = null;
37+
38+
public TestSecurityServiceFactory()
39+
{
40+
HibernateUtil.initialise(null);
41+
PropertyManager propMgr = PropertyManager.getInstance();
42+
String propFileName = IS_PROVDER ? PROV_PROP_FILE_NAME : CONS_PROP_FILE_NAME;
43+
propMgr.loadPropertyFile(propFileName);
44+
45+
properties = propMgr.getProperties(propFileName);
46+
}
47+
48+
public void testGetInstance()
49+
{
50+
ExternalSecurityService extService = SecurityServiceFactory.getSecurityService("BeareR", AdapterType.CONSUMER);
51+
if (extService != null)
52+
{
53+
AbstractSecurityService abstractService = extService.getSecurityService(properties);
54+
if (abstractService != null)
55+
{
56+
if (IS_PROVDER)
57+
{
58+
TokenInfo tokenInfo = abstractService.getInfo("VGVzdFNJUzoyMDE1LTA3LTE0VDE1OjE5OjAxWg==", null);
59+
System.out.println(tokenInfo);
60+
}
61+
else
62+
{
63+
TokenCoreInfo coreInfo = new TokenCoreInfo();
64+
coreInfo.setAppUserInfo(new EnvironmentKey(null, properties.getPropertyAsString("env.application.key", null)));
65+
// TokenInfo tokenInfo = service.generateToken(coreInfo, properties.getPropertyAsString("env.pwd", null));
66+
TokenInfo tokenInfo = abstractService.createToken(coreInfo, properties.getPropertyAsString("env.pwd", null));
67+
System.out.println(tokenInfo);
68+
}
69+
}
70+
else
71+
{
72+
System.out.println("Returned Security Service Implementation is null.");
73+
}
74+
}
75+
else
76+
{
77+
System.out.println("Returned Security Service from Factory is null.");
78+
}
79+
}
80+
81+
public static void main(String[] args)
82+
{
83+
TestSecurityServiceFactory tester = new TestSecurityServiceFactory();
84+
85+
System.out.println("Start Testing TestSecurityServiceFactory...");
86+
87+
tester.testGetInstance();
88+
89+
System.out.println("End Testing TestSecurityServiceFactory.");
90+
}
91+
}

0 commit comments

Comments
 (0)