001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.users;
019
020import io.stallion.dataAccess.DataAccessRegistry;
021import io.stallion.dataAccess.DataAccessRegistration;
022import io.stallion.dataAccess.LocalMemoryStash;
023import io.stallion.dataAccess.NoStash;
024import io.stallion.dataAccess.StandardModelController;
025import io.stallion.dataAccess.db.DB;
026import io.stallion.dataAccess.db.DbPersister;
027import io.stallion.settings.Settings;
028import org.apache.commons.lang3.StringUtils;
029
030import java.util.Map;
031
032import static io.stallion.utils.Literals.*;
033
034
035public class OAuthClientController extends StandardModelController<OAuthClient> {
036    private Map<String, OAuthClient> builtinClientsByFullKey = map();
037    private Map<Long, OAuthClient> builtinClientsById = map();
038
039    public static OAuthClientController instance() {
040        return (OAuthClientController) DataAccessRegistry.instance().get("oauth_clients");
041    }
042
043    public static void register() {
044        DataAccessRegistration registration = new DataAccessRegistration()
045                .setBucket("oauth_clients")
046                .setControllerClass(OAuthClientController.class)
047                .setModelClass(OAuthClient.class)
048                ;
049        if (DB.available()) {
050            registration
051                    .setStashClass(NoStash.class)
052                    .setTableName("oauth_clients")
053                    .setPersisterClass(DbPersister.class);
054        } else {
055            registration
056                    .setStashClass(LocalMemoryStash.class)
057                    .setPath("oauth_clients")
058                    .setShouldWatch(false);
059
060        }
061        DataAccessRegistry.instance().register(registration);
062
063        // Add in the default clients...
064        if (!empty(Settings.instance().getoAuth().getClients())) {
065            for(OAuthClient client: Settings.instance().getoAuth().getClients()) {
066                if (empty(client.getFullClientId())) {
067                    client.setFullClientId(client.getId().toString());
068                }
069                instance().builtinClientsByFullKey.put(client.getFullClientId(), client);
070                instance().builtinClientsById.put(client.getId(), client);
071            }
072        }
073
074    }
075
076    public OAuthClient forId(Long id) {
077        if (builtinClientsById.containsKey(id)) {
078            return builtinClientsById.get(id);
079        }
080        return super.forId(id);
081    }
082
083
084    public OAuthClient clientForFullId(String fullClientId) {
085        if (builtinClientsByFullKey.containsKey(fullClientId)) {
086            return builtinClientsByFullKey.get(fullClientId);
087        }
088        Long clientId = fullIdToLongId(fullClientId);
089        OAuthClient client = null;
090        if (empty(clientId)) {
091            client = forUniqueKey("fullClientId", fullClientId);
092        } else {
093            client = forId(clientId);
094            if (client != null && !client.getFullClientId().equals(fullClientId)) {
095                client = null;
096            }
097        }
098        return client;
099    }
100
101    public static long fullIdToLongId(String fullClientId) {
102        String idPart = "";
103        if (!fullClientId.contains("-")) {
104            idPart = fullClientId;
105        } else {
106            idPart = StringUtils.split(fullClientId, "-", 2)[0];
107        }
108        if (!StringUtils.isNumeric(idPart)) {
109            return 0;
110        }
111        return Long.parseLong(idPart);
112    }
113
114
115}