AuthenticateScript.groovy revision f2b76022e244a5f1078d49d4d098f1f686062f62
4b3769ce483ece06f60f983193712492b920144fJake Feasel/*
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4b3769ce483ece06f60f983193712492b920144fJake Feasel *
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici * Copyright (c) 2014 ForgeRock AS. All Rights Reserved
4b3769ce483ece06f60f983193712492b920144fJake Feasel *
4b3769ce483ece06f60f983193712492b920144fJake Feasel * The contents of this file are subject to the terms
4b3769ce483ece06f60f983193712492b920144fJake Feasel * of the Common Development and Distribution License
4b3769ce483ece06f60f983193712492b920144fJake Feasel * (the License). You may not use this file except in
4b3769ce483ece06f60f983193712492b920144fJake Feasel * compliance with the License.
4b3769ce483ece06f60f983193712492b920144fJake Feasel *
4b3769ce483ece06f60f983193712492b920144fJake Feasel * You can obtain a copy of the License at
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici * http://forgerock.org/license/CDDLv1.0.html
4b3769ce483ece06f60f983193712492b920144fJake Feasel * See the License for the specific language governing
4b3769ce483ece06f60f983193712492b920144fJake Feasel * permission and limitations under the License.
4b3769ce483ece06f60f983193712492b920144fJake Feasel *
4b3769ce483ece06f60f983193712492b920144fJake Feasel * When distributing Covered Code, include this CDDL
4b3769ce483ece06f60f983193712492b920144fJake Feasel * Header Notice in each file and include the License file
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici * at http://forgerock.org/license/CDDLv1.0.html
4b3769ce483ece06f60f983193712492b920144fJake Feasel * If applicable, add the following below the CDDL Header,
4b3769ce483ece06f60f983193712492b920144fJake Feasel * with the fields enclosed by brackets [] replaced by
4b3769ce483ece06f60f983193712492b920144fJake Feasel * your own identifying information:
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici * "Portions Copyrighted [year] [name of copyright owner]"
4b3769ce483ece06f60f983193712492b920144fJake Feasel *
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici * @author Gael Allioux <gael.allioux@forgerock.com>
4b3769ce483ece06f60f983193712492b920144fJake Feasel */
4b3769ce483ece06f60f983193712492b920144fJake Feasel
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport groovy.sql.Sql
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport org.forgerock.openicf.connectors.scriptedsql.ScriptedSQLConfiguration
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport org.forgerock.openicf.misc.scriptedcommon.OperationType
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport org.identityconnectors.common.logging.Log
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport org.identityconnectors.common.security.GuardedString
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport org.identityconnectors.common.security.SecurityUtil
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport org.identityconnectors.framework.common.exceptions.InvalidPasswordException
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport org.identityconnectors.framework.common.objects.ObjectClass
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport org.identityconnectors.framework.common.objects.OperationOptions
4b3769ce483ece06f60f983193712492b920144fJake Feasel
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport java.sql.Connection
4b3769ce483ece06f60f983193712492b920144fJake Feasel
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef operation = operation as OperationType
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef configuration = configuration as ScriptedSQLConfiguration
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef connection = connection as Connection
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef username = username as String
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef log = log as Log
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef objectClass = objectClass as ObjectClass
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef options = options as OperationOptions
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef password = password as GuardedString;
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricilog.info("Entering " + operation + " Script");
4b3769ce483ece06f60f983193712492b920144fJake Feaseldef sql = new Sql(connection);
4b3769ce483ece06f60f983193712492b920144fJake Feaseldef authId = null;
4b3769ce483ece06f60f983193712492b920144fJake Feasel
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricisql.eachRow("SELECT id FROM Users WHERE uid = ? AND password = sha1(?)", [username, SecurityUtil.decrypt(password)]) {
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici authId = String.valueOf(it.id)
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici}
4b3769ce483ece06f60f983193712492b920144fJake Feasel
4b3769ce483ece06f60f983193712492b920144fJake Feaselif (authId == null) {
4b3769ce483ece06f60f983193712492b920144fJake Feasel throw new InvalidPasswordException("Authentication Failed")
4b3769ce483ece06f60f983193712492b920144fJake Feasel}
4b3769ce483ece06f60f983193712492b920144fJake Feasel
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricireturn authId