-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from telefonicaid/feature/225_add_cache_oauth…
…2_hive_auth_provider feature/225_add_cache_oauth2_hive_auth_provider
- Loading branch information
Showing
6 changed files
with
335 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
- [cosmos-hive-auth-provider][hardening] Improve logs (#227) | ||
- [cosmos-hive-auth-provider][feature] Add OAuth2 cache (#225) |
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
118 changes: 118 additions & 0 deletions
118
...-auth-provider/src/main/java/com/telefonica/iot/cosmos/hive/authprovider/OAuth2Cache.java
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Copyright 2017 Telefonica Investigación y Desarrollo, S.A.U | ||
* | ||
* This file is part of fiware-cosmos (FI-WARE project). | ||
* | ||
* fiware-cosmos is free software: you can redistribute it and/or modify it under the terms of the GNU Affero | ||
* General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your | ||
* option) any later version. | ||
* fiware-cosmos is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the | ||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License along with fiware-cosmos. If not, see | ||
* http://www.gnu.org/licenses/. | ||
* | ||
* For those usages not covered by the GNU Affero General Public License please contact with iot_support at tid dot es | ||
*/ | ||
package com.telefonica.iot.cosmos.hive.authprovider; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.HashMap; | ||
import java.util.Properties; | ||
import org.apache.log4j.Logger; | ||
|
||
/** | ||
* | ||
* @author frb | ||
*/ | ||
public class OAuth2Cache { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(OAuth2Cache.class); | ||
private final String cacheFile; | ||
private final HashMap<String, String> cache; | ||
|
||
/** | ||
* Constructor. | ||
* @param cacheFile | ||
*/ | ||
public OAuth2Cache(String cacheFile) { | ||
this.cacheFile = cacheFile; | ||
cache = new HashMap<>(); | ||
loadCacheFromFile(); | ||
} // OAuth2Cache | ||
|
||
/** | ||
* Gets if the pair (user, token) is cached or not. | ||
* @param user | ||
* @param token | ||
* @return True if the pair (user, token) is cached, otherwise false | ||
*/ | ||
public boolean isCached(String user, String token) { | ||
if (cache == null || cache.isEmpty()) { | ||
return false; | ||
} else { | ||
String storedToken = cache.get(user); | ||
|
||
if (storedToken == null) { | ||
return false; | ||
} else { | ||
return token.equals(storedToken); | ||
} // if else | ||
} // if else | ||
} // isCached | ||
|
||
/** | ||
* Adds the pais (user, token) to the cache. | ||
* @param user | ||
* @param token | ||
*/ | ||
public void addToCache(String user, String token) { | ||
cache.put(user, token); | ||
saveCacheToFile(); | ||
} // addToCache | ||
|
||
private void loadCacheFromFile() { | ||
Properties props = new Properties(); | ||
|
||
try { | ||
props.load(new FileInputStream(cacheFile)); | ||
} catch (FileNotFoundException e) { | ||
LOGGER.error("Cache properties file not found when reading. Details: " + e.getMessage()); | ||
return; | ||
} catch (IOException e) { | ||
LOGGER.error("There was a problem while reading the cache properties file. Details: " | ||
+ e.getMessage()); | ||
return; | ||
} // try catch | ||
|
||
for (String key : props.stringPropertyNames()) { | ||
String value = props.getProperty(key); | ||
cache.put(key, value); | ||
} // for | ||
} // loadCacheFromFile | ||
|
||
private void saveCacheToFile() { | ||
PrintWriter writer; | ||
|
||
try { | ||
writer = new PrintWriter(new FileOutputStream(cacheFile)); | ||
} catch (FileNotFoundException e) { | ||
LOGGER.error("Cache properties file not found when saving. Details: " + e.getMessage()); | ||
return; | ||
} // try catch | ||
|
||
for (String key : cache.keySet()) { | ||
String value = cache.get(key); | ||
writer.println(key + "=" + value); | ||
} // for | ||
|
||
writer.flush(); | ||
writer.close(); | ||
} // saveCacheToFile | ||
|
||
} // OAuth2Cache |
28 changes: 28 additions & 0 deletions
28
...h-provider/src/main/java/com/telefonica/iot/cosmos/hive/authprovider/utils/Constants.java
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright 2017 Telefonica Investigación y Desarrollo, S.A.U | ||
* | ||
* This file is part of fiware-cosmos (FI-WARE project). | ||
* | ||
* fiware-cosmos is free software: you can redistribute it and/or modify it under the terms of the GNU Affero | ||
* General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your | ||
* option) any later version. | ||
* fiware-cosmos is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the | ||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License along with fiware-cosmos. If not, see | ||
* http://www.gnu.org/licenses/. | ||
* | ||
* For those usages not covered by the GNU Affero General Public License please contact with iot_support at tid dot es | ||
*/ | ||
package com.telefonica.iot.cosmos.hive.authprovider.utils; | ||
|
||
/** | ||
* | ||
* @author frb | ||
*/ | ||
public class Constants { | ||
|
||
public static final String CACHEBACKUPFILE = "/home/hive/oauth2.cache"; | ||
|
||
} // Constants |
Oops, something went wrong.