4b3769ce483ece06f60f983193712492b920144fJake Feasel/*
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4b3769ce483ece06f60f983193712492b920144fJake Feasel *
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici * Copyright (c) 2015 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 *
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici * Version 1.0
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici * Author ForgeRock
4b3769ce483ece06f60f983193712492b920144fJake Feasel */
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Bricipackage org.forgerock.openicf.connectors.hrdb
4b3769ce483ece06f60f983193712492b920144fJake Feasel
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport groovy.sql.Sql
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Briciimport org.forgerock.openicf.connectors.hrdb.HRDBConfiguration
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
4b3769ce483ece06f60f983193712492b920144fJake Feasel
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Briciimport java.sql.Connection
4b3769ce483ece06f60f983193712492b920144fJake Feasel
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici/**
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici * Built-in accessible objects
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici **/
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici// OperationType is AUTHENTICATION for this script
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef operation = operation as OperationType
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici// The configuration class created specifically for this connector
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Bricidef configuration = configuration as HRDBConfiguration
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici// Default logging facility
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef log = log as Log
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici/**
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici * Script action - Customizable
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici *
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici * Must either return an int or String convertible to a Uid object or throw an exception
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici **/
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Bricidef authId = null;
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici/**
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici * Params not generated custom connector tool
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici **/
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici// username used in the sql query
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Bricidef username = username as String
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici// password used in the sql query
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricidef password = password as GuardedString;
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici// connection used for connecting to the SQL repo
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Bricidef connection = connection as Connection
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
f2b76022e244a5f1078d49d4d098f1f686062f62Alin Bricilog.info("Entering " + operation + " Script");
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici// create connection to SQL
4b3769ce483ece06f60f983193712492b920144fJake Feaseldef sql = new Sql(connection);
4b3769ce483ece06f60f983193712492b920144fJake Feasel
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici// do select with provided, username/password
185941104719f48494a107996e17a23aad237c7dJon Branchsql.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
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Brici// check if user was returned..would imply authenticated
4b3769ce483ece06f60f983193712492b920144fJake Feaselif (authId == null) {
4b3769ce483ece06f60f983193712492b920144fJake Feasel throw new InvalidPasswordException("Authentication Failed")
4b3769ce483ece06f60f983193712492b920144fJake Feasel}
4b3769ce483ece06f60f983193712492b920144fJake Feasel
db4f02cbee52b92fd37a39aeee44dcd2272bc99cAlin Bricireturn authId