view flys-client/src/main/java/de/intevation/flys/client/server/auth/plain/Authenticator.java @ 2980:cc126abafeab

Cosmetics in plain authentication flys-client/trunk@4978 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 13 Jul 2012 09:42:38 +0000
parents 98514ab2c9ba
children 1f64ee424f92
line wrap: on
line source
package de.intevation.flys.client.server.auth.plain;

import de.intevation.flys.client.server.auth.AuthenticationException;
import de.intevation.flys.client.server.auth.DefaultUser;
import de.intevation.flys.client.server.auth.User;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

public class Authenticator
implements   de.intevation.flys.client.server.auth.Authenticator
{
    public static class Authentication
    implements          de.intevation.flys.client.server.auth.Authentication
    {
        protected String       user;
        protected String       password;
        protected List<String> roles;

        public Authentication(
            String       user,
            String       password,
            List<String> roles
        ) {
            this.user     = user;
            this.password = password;
            this.roles    = roles;
        }

        @Override
        public boolean isSuccess() {
            return user != null;
        }

        @Override
        public User getUser() {
            return isSuccess()
                ? new DefaultUser(user, password, false, roles)
                : null;
        }
    } // class Authentication

    public Authenticator() {
    }

    private static File credentialsFile() {
        String env = System.getenv("FLYS_USER_FILE");
        if (env == null) {
            env = System.getProperty(
                "flys.user.file",
                System.getProperty("user.home", ".")
                + System.getProperty("file.separator")
                + "flys_user_file");
        }
        return new File(env);

    }

    @Override
    public de.intevation.flys.client.server.auth.Authentication auth(
        String username,
        String password,
        String encoding
    )
    throws AuthenticationException, IOException
    {
        File file = credentialsFile();
        if (!file.canRead() || !file.isFile()) {
            return new Authentication(null, null, new ArrayList<String>(0));
        }

        BufferedReader reader =
            new BufferedReader(
            new FileReader(file));
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                if ((line = line.trim()).length() == 0
                || line.startsWith("#")) {
                    continue;
                }
                String [] parts = line.split("\\s+");
                if (parts.length < 2) {
                    continue;
                }
                if (parts[0].equals(username)) {
                    if (parts[1].equals(password)) {
                        List<String> roles =
                            new ArrayList<String>(parts.length - 2);

                        for (int i = 2; i < parts.length; i++) {
                            roles.add(parts[i]);
                        }

                        return new Authentication(username, password, roles);
                    }
                    // Stop: user found, wrong password
                    break;
                }
            }
        }
        finally {
            reader.close();
        }
        return new Authentication(null, null, new ArrayList<String>(0));
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org