diff --git a/plugins/stoa/Gemfile b/plugins/stoa/Gemfile new file mode 100644 index 0000000..e14b21b --- /dev/null +++ b/plugins/stoa/Gemfile @@ -0,0 +1 @@ +gem 'sinatra' diff --git a/plugins/stoa/config.ru b/plugins/stoa/config.ru new file mode 100644 index 0000000..440f1e3 --- /dev/null +++ b/plugins/stoa/config.ru @@ -0,0 +1,5 @@ +require ::File.expand_path('../../../config/environment', __FILE__) +require 'stoa_plugin' +require 'stoa_plugin/auth' + +run StoaPlugin::Auth diff --git a/plugins/stoa/lib/stoa_plugin/auth.rb b/plugins/stoa/lib/stoa_plugin/auth.rb new file mode 100644 index 0000000..fa4ee67 --- /dev/null +++ b/plugins/stoa/lib/stoa_plugin/auth.rb @@ -0,0 +1,36 @@ +require 'sinatra' +require 'stoa_plugin/person_fields' + +class StoaPlugin::Auth < Sinatra::Base + + include StoaPlugin::PersonFields + + post '/' do + headers['Content-Type'] = 'application/json' + if params[:login].blank? + person = Person.find_by_usp_id(params[:usp_id]) + login = person ? person.user.login : nil + else + login = params[:login] + end + + domain = Domain.find_by_name(request.host) + environment = domain && domain.environment + environment ||= Environment.default + + user = User.authenticate(login, params[:password], environment) + if user + result = StoaPlugin::PersonApi.new(user.person, self).fields(selected_fields(params[:fields], user)) + result.merge!(:ok => true) + else + result = { :error => _('Incorrect user/password pair.'), :ok => false } + end + result.to_json + end + + get '/' do + headers['Content-Type'] = 'application/json' + { :error => _('Conection requires post method.'), :ok => false }.to_json + end + +end diff --git a/plugins/stoa/script/stoa-auth-server b/plugins/stoa/script/stoa-auth-server new file mode 100755 index 0000000..dc0d0bf --- /dev/null +++ b/plugins/stoa/script/stoa-auth-server @@ -0,0 +1,123 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: noosfero +# Required-Start: $remote_fs +# Required-Stop: $remote_fs +# Should-Start: postgresql +# Should-Stop: postgresql +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Stoa authentication daemon +# Description: This file should be symlinked or copied into /etc/init.d. +### END INIT INFO + +# Sample init.d script for noosfero. +# +# This script was based on the skeleton init.d script present in a Debian +# GNU/Linux system (sid), on Sat Feb 16 11:12:03 BRT 2008. It must be placed +# in /etc/init.d/ (or whatever place your system uses for startup scripts), +# and you must create a file /etc/default/noosfero defining the variable +# +# Author: Antonio Terceiro + +# Do NOT "set -e" + +# PATH should only include /usr/* if it runs after the mountnfs.sh script +PATH=/sbin:/usr/sbin:/bin:/usr/bin +DESC="Stoa authentication deamon" +NAME=stoa-auth-daemon +SCRIPTNAME=/etc/init.d/$NAME + +# default values +NOOSFERO_DIR=/var/lib/noosfero/current +NOOSFERO_USER=noosfero + +# Read configuration variable file if it is present +if [ -r /etc/default/$NAME ]; then + . /etc/default/$NAME +else + # for running from development setup, or from git with the script symlinked + script=$(readlink -f $0) + NOOSFERO_DIR=$(readlink -f $(dirname $script)/../../..) + NOOSFERO_USER=$(stat --format %U $NOOSFERO_DIR/tmp/pids) +fi + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +. /lib/lsb/init-functions + +if [ -z "$NOOSFERO_DIR" ] || [ -z "$NOOSFERO_USER" ]; then + echo "NOOSFERO_DIR or NOOSFERO_USER not defined, noosfero not being started." + echo "Both variables must be defined in /etc/default/noosfero" + exit 0 +fi + +if test -x /usr/sbin/noosfero-check-dbconfig ; then + if ! /usr/sbin/noosfero-check-dbconfig; then + echo "Noosfero database access not configured, service disabled." + exit 0 + fi +fi + +###################### + +THIN_PID_FILE='--pid tmp/pids/stoa-auth-server.pid' +THIN_START_OPTIONS="--port 4000 --environment production --chdir $NOOSFERO_DIR --rackup plugins/stoa/config.ru --log log/stoa-auth-server.log --daemonize" + +as_noosfero_user() { + cmd="$@" + if [ "$NOOSFERO_USER" != "$USER" ]; then + su $NOOSFERO_USER -l -c "cd $NOOSFERO_DIR && $cmd" + else + cd $NOOSFERO_DIR + $cmd + fi +} + +run_thin() { + action="$1" + shift + log_daemon_msg "$action $DESC" "$NAME" + if as_noosfero_user thin $THIN_PID_FILE $@; then + log_success_msg + else + log_failure_msg + fi +} + +running() { + kill -0 $(cat $NOOSFERO_DIR/tmp/pids/stoa-auth-server.pid 2>/dev/null || true) 2>/dev/null +} + +do_start() { + if ! running; then + run_thin "Starting" $THIN_START_OPTIONS start + fi +} + +do_stop() { + if running; then + run_thin "Stopping" stop + fi +} + +do_restart() { + if running; then + do_stop + fi + do_start +} + +case "$1" in + start|stop|restart) + do_$1 + ;; + force-reload) + do_restart + ;; + *) + echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|setup}" >&2 + exit 3 + ;; +esac -- libgit2 0.21.2