Definizione del modello ThingWorx in Composer > Protezione > Autenticatori > Autenticatori del dispositivo edge > Esempi > Esempio di estensione dell'autenticatore del dispositivo edge
Esempio di estensione dell'autenticatore del dispositivo edge
L'esempio riportato di seguito illustra come è possibile sviluppare l'estensione dell'autenticatore di un dispositivo edge per utilizzare le attestazioni di protezione inviate dal dispositivo edge.
Questo esempio esegue alcune operazioni, tra cui quelle riportate di seguito.
Estende la classe SecurityClaimsAuthenticator dell'API di ThingWorx Platform per implementare la logica di elaborazione personalizzata nei metodi authenticate e matchesAuthRequest.
Nel metodo matchesAuthRequest si verifica quanto riportato di seguito.
Vengono controllate le attestazioni di protezione in entrata per verificare che contengano un'attestazione secretTokenKey.
Se contengono un'attestazione secretTokenKey, il metodo restituisce true alla piattaforma, a indicare che l'autenticatore deve essere utilizzato per convalidare le attestazioni di protezione.
Nel metodo authenticate si verifica quanto riportato di seguito.
Ottiene secretTokenKey.
Se secretTokenKey è diverso da null e il campo non è vuoto, e se il relativo valore corrisponde al valore previsto di MySecretKey, il metodo restituisce il valore di whoTheySaidTheyWere, che corrisponde a un utente.
Se l'utente specificato esiste, viene autenticato in ThingWorx Platform.
package your.company.thingworx.authenticator;

import com.thingworx.communications.common.SecurityClaims;
import com.thingworx.security.authentication.AuthenticatorException;
import com.thingworx.security.authentication.SecurityClaimsAuthenticator;

/**
* Custom edge security authenticators are based around the "SecurityClaims"
* class. This class is at its essence a wrapper around a map. To use the
* map, one provides
*/
public class CustomEdgeAuthenticator extends SecurityClaimsAuthenticator {

private static final long serialVersionUID = 1L;

/**
* The matchesAuthRequest method is the indicator that this
* authenticator should be used to verify the SecurityClaims object
* presented. This method allows the author to make a determination
* about the claims provided by inspecting the information therein and
* decide if this authenticator
* should be used to try to verify the claims provided or not.
*
* @param SecurityClaims object containing the corresponding security
* information @return boolean indicating the authenticator should be
* used to verify the SecurityClaims provided
*/
@Override
public boolean matchesAuthRequest(SecurityClaims securityClaims) throws
AuthenticatorException {
//in this simple implementation we are looking for a 'secretTokenKey'
//claim. If one is present in the securityClaims object supplied -
//this SecurityClaimsAuthenticator should be used
return securityClaims.getClaims().containsKey("secretTokenKey");
}

/**
* This is the method that must be implemented to ensure the presented
* SecurityClaims are valid.
*
* @param SecurityClaims object containing the corresponding security
* information @return String indicating the corresponding user identity
* which the SecurityClaims represent.
*/
@Override
public String authenticate(SecurityClaims securityClaims) throws
AuthenticatorException {
String secretTokenValue = securityClaims.getClaims().get
("secretTokenKey");

if(secretTokenValue != null && secretTokenValue.trim().length() > 0)
{
//the secretTokenValue was provided - verify it is the proper
value!
if("MySecretKey".equals(secretTokenValue)) {
//Only when all these checks are complete will we consider
// the auth request to be valid. Here we have permitted the
// user to tell the platform "who" they are via the
// SecurityClaims
//This is a naieve example - and is for illustrative
// purposes only.
return securityClaims.getClaims().get("whoTheySaidTheyWere");
}
}

//throw a generic exception - and do not leak important information
//to the caller
throw new AuthenticatorException("The claims provided are incorrect");
}
}
È stato utile?