Skip to main content

External Authorization

How to perform external authorization via API?

Written by Petr Pech

The ABRA Flexi accounting system normally authenticates users against an internal user database. It is also possible to authenticate against external systems (e.g. LDAP). For this purpose, we have prepared a Java interface that enables this functionality. Code written this way will run as part of the server and allow authentication against external systems.

Note: this interface is experimental and cannot be used in the cloud. If you would like to use this feature, please contact us.

The foundation of everything is the cz.winstrom.auth.Auth interface:

cz.winstrom.auth.Auth</code>:</p>

<pre class="brush: java">package cz.winstrom.auth;

import cz.winstrom.vo.UserInfo;
import java.sql.Connection;

/**
*
* Základní rozhraní pro ověřování uživatelů proti jiným službám. Její nastavení se provede pomocí nastavení flexibee-server.xml:
*
* &lt;entry key="authClass"&gt;cz.winstrom.auth.AuthSimple&lt;/entry&gt;
*
* Jako parametr je zde název třídy, kterou je nutné dostat do CLASSPATH serveru.
*
* V tuto chvíli umí aplikace pouze ověření plain heslem.
*
* Kvůli změnám v tomto rozhraní doporučujeme, aby implementátoři dědily od abstraktní třídy "AuthBase".
*
* @author fers
*/
public interface Auth {
/**
* Autorizuj uživatele dle jména a hesla.
*
* @param connection napojení do databáze centralServer
* @param userInfo informace o uživateli z centralServer.csuzivatel. Pokud je null, žádný takový uživatel neexistuje v hlavní databázi uživatelů ABRA Flexi. Při úspěšné autorizaci bude vytvořen.
* @param username jméno uživatele
* @param password heslo uživatele
* @return true pokud bylo heslo zadáno správně.
*/
boolean authenticate(Connection connection, UserInfo userInfo, String username, String password);

/**
* Pokud se povede autorizace, ale uživatel neexistuje v databází centralServer.csuzivatel, bude založen. Autorizační systém může ovlivnit informace o založeném uživateli tím,
* že vyplní třídu UserInfo.
*
* ID se nesmí měnit (má hodnotu -1) i heslo doporučujeme nevyplňovat, protože ověření proběhne vždy vúči autorizačnímu systému.
*
* Obvykle se vyplňují pouze přístupová práva (založení firmy, smazání firmy, ...)
*
* @param connection napojení do databáze centralServer
* @param userInfo informace o uživateli, které by měli být vráceny (může být vrácena i jiná instance, ale musí být správně vyplněna.
* @return informace o uživateli, které budou uloženy do databáze. Obvykle se vrací upravený parametr userInfo.
*/
public UserInfo getUserInfo(Connection connection, UserInfo userInfo);

...
}

However, for direct implementation always use inheritance from the cz.winstrom.auth.AuthBase or cz.winstrom.auth.AuthBaseV2 class, which allows us to modify this interface while maintaining backward compatibility and to pre-implement certain methods.

The actual implementation of the authorization class may look like this:

public class AuthSimple extends AuthBase {

@Override
public boolean authenticate(Connection connection, UserInfo userInfo, String username, String password) {
// jednoduché ověření: jméno a heslo se rovnají
return username.equalsIgnoreCase(password);
}

@Override
public UserInfo getUserInfo(Connection connection, UserInfo userInfo) {
userInfo.setCreateCompany(true); // novému uživateli povolíme založení firmy

return userInfo;
}
}

ABRA Flexi supports security via two-factor authentication. This functionality is available in the newer cz.winstrom.auth.AuthBaseV2 class. The following example assumes that you leave the 2FA password verification to the default implementation in Flexi:

public class OTPAuth extends AuthBaseV2 {

@Override
public boolean authenticate(UserAuthenticationFacade facade, String password, String otp) throws WSNotAuthorizedException, WSBlockedException {
String username = facade.getUsername();
return username.equals("admin") && password.equals("admin") && facade.verifyOtp(otp);
}

@Override
public boolean isTwoPhaseAuthEnabled(UserAuthenticationFacade facade, String username) {

final UserInfo userInfo = facade.getUserInfo();
return userInfo.isTwoPhaseAuthEnabled();
}
}

The class to be used for server authentication is determined by the <entry key="authClass">cz.winstrom.auth.AuthSimple</entry> variable in the flexibee-server.xml file.

Additional files must be added to the classpath. On Linux, for example, this can be done by editing the /etc/default/flexibee file, uncommenting and populating the CLASSPATH variable:

CLASSPATH="cesta/ke/knihovne-1.0.jar:cesta/k/auth.jar"
Did this answer your question?