stormpath / stormpath-shiro

Apache Shiro plugin for Stormpath
Apache License 2.0
34 stars 31 forks source link

Coming from a support question: It would be nice to provide OS-independent ApiKey File Reader #18

Open mrioan opened 10 years ago

mrioan commented 10 years ago

One user had the problem that the shiro.ini in Windows was:

stormpathClient.apiKeyFileLocation = C:/stormpathApiKey.properties 

while in Linux:

stormpathClient.apiKeyFileLocation = /home/user/stormpathApiKey.properties 

We might want to provide a way to read properties in an OS-independent way.

BTW, I suggested to user to do:

public class ApiKeyReader extends Reader {
    private static Logger logger = LoggerFactory.getLogger(ApiKeyReader.class);

    private FileReader reader;

    public ApiKeyReader() {
        try {
            String osName = System.getProperty("os.name");
            if(osName.contains("Mac OS") || osName.contains("linux")) {
                reader = new FileReader("/Users/mario/.stormpath/api1Key.properties");
            } else if(osName.contains("windows")) {
                reader = new FileReader("c:/apiKey.properties");
            } else {
                throw new RuntimeException("Unrecognized OS: " + osName);
            }
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage());
        }

    }

    @Override
    public int read(char[] chars, int i, int i2) throws IOException {
        return reader.read(chars, i, i2);
    }

    @Override
    public void close() throws IOException {
        reader.close();
    }
}

Then, in shiro.ini change this:

stormpathClient.apiKeyFileLocation = C:/stormpathApiKey.properties

to

apikeyReader = com.stormpath.test.ApiKeyReader 
stormpathClient.apiKeyReader = $apikeyReader
descmdr commented 10 years ago

Another option is to be able to place the file relative to the shiro.ini file. I'm not sure how to do that the "right way", but my own webapp is organized such that "classpath:../apiKey.properties" works.