Saltar para o conteúdo principal
Versão: v19

Autenticação

Authenticating users is necessary when you want to provide specific access rights to web users. Authentication designates the way the information concerning the user credentials (usually name and password) are collected and processed.

Modos de autenticação

The 4D web server proposes three authentication modes, that you can select in the Web/Options (I) page of the Settings dialog box:

É recomendado usar uma autenticação personalizada.

Visão Geral

O funcionamento do sistema de acesso do servidor web 4D está resumido no diagrama seguinte:

Requests starting with rest/ are directly handled by the REST server.

Personalizado (padrão)

Basically in this mode, it's up to the developer to define how to authenticate users. 4D só avalia solicitações HTTP que requerem uma autenticação.

Este modo de autenticação é o mais flexível porque permite que você:

  • either, delegate the user authentication to a third-party application (e.g. a social network, SSO);
  • or, provide an interface to the user (e.g. a web form) so that they can create their account in your customer database; then, you can authenticate users with any custom algorithm (see this example from the "User sessions" chapter). The important thing is that you never store the password in clear, using such code:
//... user account creation
ds.webUser.password:=Generate password hash($password)
ds.webUser.save()

See also this example from the "Getting started" chapter.

If no custom authentication is provided, 4D calls the On Web Authentication database method (if it exists). In addition to $1 and $2, only the IP addresses of the browser and the server ($3 and $4) are provided, the user name and password ($5 and $6) are empty. The method must return True in $0 if the user is successfully authenticated, then the resquested resource is served, or False in $0 if the authentication failed.

Warning: If the On Web Authentication database method does not exist, connections are automatically accepted (test mode).

Protocolo Basic

When a user connects to the server, a standard dialog box appears on their browser in order for them to enter their user name and password.

O nome e a palavra-passe introduzidos pelo utilizador são enviados sem encriptação no cabeçalho do pedido HTTP. Este modo requer normalmente HTTPS para garantir a confidencialidade.

Os valores introduzidos são então avaliados:

  • If the Include 4D passwords option is checked, user credentials will be first evaluated against the internal 4D users table.

    • If the user name sent by the browser exists in the table of 4D users and the password is correct, the connection is accepted. Se a palavra-passe estiver incorreta, a ligação é recusada.
    • If the user name does not exist in the table of 4D users, the On Web Authentication database method is called. If the On Web Authentication database method does not exist, connections are rejected.
  • If the Include 4D passwords option is not checked, user credentials are sent to the On Web Authentication database method along with the other connection parameters (IP address and port, URL...) so that you can process them. If the On Web Authentication database method does not exist, connections are rejected.

    With the 4D Client web server, keep in mind that all the sites published by the 4D Client machines will share the same table of users. Validação de usuários/senhas é realizada pela aplicação 4D Server.

Protocolo DIGEST

This mode provides a greater level of security since the authentication information is processed by a one-way process called hashing which makes their contents impossible to decipher.

Como no modo BASIC, os usuários devem digitar seu nome e senha ao se conectarem. The On Web Authentication database method is then called. When the DIGEST mode is activated, the $6 parameter (password) is always returned empty. In fact, when using this mode, this information does not pass by the network as clear text (unencrypted). It is therefore imperative in this case to evaluate connection requests using the WEB Validate digest command.

You must restart the web server in order for the changes made to these parameters to be taken into account.

On Web Authentication

The On Web Authentication database method is in charge of managing web server engine access. É chamado por 4D ou 4D Server quando uma solicitação HTTP dinâmica é recebida.

Chamadas métodos de base

The On Web Authentication database method is automatically called when a request or processing requires the execution of some 4D code (except for REST calls). It is also called when the web server receives an invalid static URL (for example, if the static page requested does not exist).

The On Web Authentication database method is therefore called:

  • when the web server receives a URL requesting a resource that does not exist
  • when the web server receives a URL beginning with 4DACTION/, 4DCGI/...
  • when the web server receives a root access URL and no home page has been set in the Settings or by means of the WEB SET HOME PAGE command
  • when the web server processes a tag executing code (e.g 4DSCRIPT) in a semi-dynamic page.

O método banco de dados On Web Authentication NÃO é chamado:

  • quando o servidor Web recebe um URL solicitando uma página estática válida.
  • when the web server reveives a URL beginning with rest/ and the REST server is launched (in this case, the authentication is handled through the On REST Authentication database method or Structure settings).

Sintaxe

On Web Authentication( $1 : Text ; $2 : Text ; $3 : Text ; $4 : Text ; $5 : Text ; $6 : Text ) -> $0 : Boolean

ParâmetrosTipoDescrição
$1Text<-URL
$2Text<-Cabeçalhos HTTP + corpo HTTP (até um limite de 32 kb)
$3Text<-Endereço IP do cliente Web (browser)
$4Text<-Endereço IP do servidor
$5Text<-Nome de usuario
$6Text<-Senha
$0Parâmetros->True = pedido aceite, False = pedido rejeitado

Estes parâmetros devem ser declarados da seguinte forma:

//Método base On Web Authentication

C_TEXT($1;$2;$3;$4;$5;$6)
C_BOOLEAN($0)

//Código do método

Alternatively, you can use the named parameters syntax:

// On Web Authentication database method
#DECLARE ($url : Text; $header : Text; \
$BrowserIP : Text; $ServerIP : Text; \
$user : Text; $password : Text) \
-> $RequestAccepted : Boolean

All the On Web Authentication database method's parameters are not necessarily filled in. The information received by the database method depends on the selected authentication mode).

$1 - URL

The first parameter ($1) is the URL received by the server, from which the host address has been removed.

Vejamos o exemplo de uma ligação Intranet. Suponha que o endereço IP do seu Web Server 4D é 123.45.67.89. The following table shows the values of $1 depending on the URL entered in the Web browser:

URL introduzido no navegador WebValor do parâmetro $1
123.45.67.89/
http://123.45.67.89/
123.45.67.89/Customers/Customers
http://123.45.67.89/Customers/Add/Customers/Add
123.45.67.89/Do_This/If_OK/Do_That/Do_This/If_OK/Do_That

$2 - Cabeçalho e corpo do pedido HTTP

O segundo parâmetro ($2) é o cabeçalho e o corpo da solicitação HTTP enviada pelo navegador Web. Note that this information is passed to your On Web Authentication database method as it is. Its contents will vary depending on the nature of the web browser which is attempting the connection.

If your application uses this information, it is up to you to parse the header and the body. Você pode usar os comandos WEB GET HTTP HEADER e WEB GET HTTP BODY.

For performance reasons, the size of data passing through the $2 parameter must not exceed 32 KB. Para além deste tamanho, são truncados pelo servidor HTTP 4D.

$3 - Endereço IP do cliente Web

The $3 parameter receives the IP address of the browser’s machine. This information can allow you to distinguish between intranet and internet connections.

4D devolve endereços IPv4 em formato híbrido IPv6/IPv4 escritos com um prefixo de 96 bits, por exemplo ::ffff:192.168.2.34 para o endereço IPv4 192.168.2.34. For more information, refer to the IPv6 Support section.

$4 - Endereço IP do servidor

The $4 parameter receives the IP address used to call the web server. 4D allows for multi-homing, which allows you to exploit machines with more than one IP address. For more information, please refer to the Configuration page.

$5 e $6 - Nome de usuário e palavra-passe

The $5 and $6 parameters receive the user name and password entered by the user in the standard identification dialog box displayed by the browser. This dialog box appears for each connection, if basic or digest authentication is selected.

If the user name sent by the browser exists in 4D, the $6 parameter (the user’s password) is not returned for security reasons.

Parâmetro $0

The On Web Authentication database method returns a boolean in $0:

  • Se $0 é True, a ligação é aceite.

  • Se $0 é False, a ligação é recusada.

The On Web Connection database method is only executed if the connection has been accepted by On Web Authentication.

WARNING
If no value is set to $0 or if $0 is not defined in the On Web Authentication database method, the connection is considered as accepted and the On Web Connection database method is executed.

      • Do not call any interface elements in the On Web Authentication database method (ALERT, DIALOG, etc.) because otherwise its execution will be interrupted and the connection refused. O mesmo acontecerá se ocorrer um erro durante seu processamento.

Exemplo

Example of the On Web Authentication database method in DIGEST mode:

 // On Web Authentication Database Method
#DECLARE ($url : Text; $header : Text; $ipB : Text; $ipS : Text; \
$user : Text; $pw : Text) -> $valid : Boolean

var $found : cs.WebUserSelection
$valid:=False

$found:=ds.WebUser.query("User === :1";$user)
If($found.length=1) // User is found
$valid:=WEB Validate digest($user;[WebUser]password)
Else
$valid:=False // User does not exist
End if