Search in sources :

Example 1 with NonPersonalizedEncryptedPayloadModel

use of io.getlime.security.powerauth.rest.api.model.entity.NonPersonalizedEncryptedPayloadModel in project powerauth-restful-integration by lime-company.

the class CustomActivationController method createNewActivation.

@RequestMapping(value = "create", method = RequestMethod.POST)
@ResponseBody
public ObjectResponse<NonPersonalizedEncryptedPayloadModel> createNewActivation(@RequestBody ObjectRequest<NonPersonalizedEncryptedPayloadModel> object) throws PowerAuthAuthenticationException, PowerAuthActivationException {
    try {
        // Check if there is any user provider to be autowired
        if (userProvider == null) {
            throw new PowerAuthActivationException();
        }
        // Prepare an encryptor
        final PowerAuthNonPersonalizedEncryptor encryptor = encryptorFactory.buildNonPersonalizedEncryptor(object);
        if (encryptor == null) {
            throw new PowerAuthActivationException();
        }
        // Decrypt the request object
        ActivationCreateCustomRequest request = encryptor.decrypt(object, ActivationCreateCustomRequest.class);
        if (request == null) {
            throw new PowerAuthActivationException();
        }
        // Lookup user ID using a provided identity
        final Map<String, String> identity = request.getIdentity();
        String userId = userProvider.lookupUserIdForAttributes(identity);
        // If no user was found, return error
        if (userId == null) {
            throw new PowerAuthActivationException();
        }
        // Create activation for a looked up user and application related to the given application key
        ActivationCreateRequest acr = request.getPowerauth();
        CreateActivationResponse response = powerAuthClient.createActivation(acr.getApplicationKey(), userId, acr.getActivationIdShort(), acr.getActivationName(), acr.getActivationNonce(), acr.getEphemeralPublicKey(), acr.getEncryptedDevicePublicKey(), acr.getExtras(), acr.getApplicationSignature());
        // Process custom attributes using a custom logic
        final Map<String, Object> customAttributes = request.getCustomAttributes();
        userProvider.processCustomActivationAttributes(customAttributes);
        // Prepare the created activation response data
        ActivationCreateResponse createResponse = new ActivationCreateResponse();
        createResponse.setActivationId(response.getActivationId());
        createResponse.setEphemeralPublicKey(response.getEphemeralPublicKey());
        createResponse.setActivationNonce(response.getActivationNonce());
        createResponse.setEncryptedServerPublicKey(response.getEncryptedServerPublicKey());
        createResponse.setEncryptedServerPublicKeySignature(response.getEncryptedServerPublicKeySignature());
        // Encrypt response object
        final ObjectResponse<NonPersonalizedEncryptedPayloadModel> powerAuthApiResponse = encryptor.encrypt(createResponse);
        // Check if activation should be committed instantly and if yes, perform commit
        if (userProvider.shouldAutoCommitActivation(identity, customAttributes)) {
            powerAuthClient.commitActivation(response.getActivationId());
        }
        // Return response
        return powerAuthApiResponse;
    } catch (IOException e) {
        throw new PowerAuthActivationException();
    }
}
Also used : CreateActivationResponse(io.getlime.powerauth.soap.CreateActivationResponse) ActivationCreateResponse(io.getlime.security.powerauth.rest.api.model.response.ActivationCreateResponse) PowerAuthNonPersonalizedEncryptor(io.getlime.security.powerauth.rest.api.base.encryption.PowerAuthNonPersonalizedEncryptor) IOException(java.io.IOException) PowerAuthActivationException(io.getlime.security.powerauth.rest.api.base.exception.PowerAuthActivationException) ActivationCreateCustomRequest(io.getlime.security.powerauth.rest.api.model.request.ActivationCreateCustomRequest) NonPersonalizedEncryptedPayloadModel(io.getlime.security.powerauth.rest.api.model.entity.NonPersonalizedEncryptedPayloadModel) ActivationCreateRequest(io.getlime.security.powerauth.rest.api.model.request.ActivationCreateRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with NonPersonalizedEncryptedPayloadModel

use of io.getlime.security.powerauth.rest.api.model.entity.NonPersonalizedEncryptedPayloadModel in project powerauth-restful-integration by lime-company.

the class CustomActivationController method createNewActivation.

@POST
@Path("create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ObjectResponse<NonPersonalizedEncryptedPayloadModel> createNewActivation(ObjectRequest<NonPersonalizedEncryptedPayloadModel> object) throws PowerAuthAuthenticationException, RemoteException, PowerAuthActivationException {
    try {
        final PowerAuthNonPersonalizedEncryptor encryptor = encryptorFactory.buildNonPersonalizedEncryptor(object);
        if (encryptor == null) {
            throw new PowerAuthActivationException();
        }
        ActivationCreateCustomRequest request = encryptor.decrypt(object, ActivationCreateCustomRequest.class);
        if (request == null) {
            throw new PowerAuthActivationException();
        }
        final Map<String, String> identity = request.getIdentity();
        String userId = userProvider.lookupUserIdForAttributes(identity);
        if (userId == null) {
            throw new PowerAuthActivationException();
        }
        ActivationCreateRequest acr = request.getPowerauth();
        PowerAuthPortServiceStub.CreateActivationResponse response = powerAuthClient.createActivation(acr.getApplicationKey(), userId, acr.getActivationIdShort(), acr.getActivationName(), acr.getActivationNonce(), acr.getEphemeralPublicKey(), acr.getEncryptedDevicePublicKey(), acr.getExtras(), acr.getApplicationSignature());
        final Map<String, Object> customAttributes = request.getCustomAttributes();
        userProvider.processCustomActivationAttributes(customAttributes);
        ActivationCreateResponse createResponse = new ActivationCreateResponse();
        createResponse.setActivationId(response.getActivationId());
        createResponse.setEphemeralPublicKey(response.getEphemeralPublicKey());
        createResponse.setActivationNonce(response.getActivationNonce());
        createResponse.setEncryptedServerPublicKey(response.getEncryptedServerPublicKey());
        createResponse.setEncryptedServerPublicKeySignature(response.getEncryptedServerPublicKeySignature());
        final ObjectResponse<NonPersonalizedEncryptedPayloadModel> powerAuthApiResponse = encryptor.encrypt(createResponse);
        if (userProvider.shouldAutoCommitActivation(identity, customAttributes)) {
            powerAuthClient.commitActivation(response.getActivationId());
        }
        return powerAuthApiResponse;
    } catch (IOException e) {
        throw new PowerAuthActivationException();
    }
}
Also used : ActivationCreateResponse(io.getlime.security.powerauth.rest.api.model.response.ActivationCreateResponse) PowerAuthNonPersonalizedEncryptor(io.getlime.security.powerauth.rest.api.base.encryption.PowerAuthNonPersonalizedEncryptor) IOException(java.io.IOException) PowerAuthActivationException(io.getlime.security.powerauth.rest.api.base.exception.PowerAuthActivationException) ActivationCreateCustomRequest(io.getlime.security.powerauth.rest.api.model.request.ActivationCreateCustomRequest) NonPersonalizedEncryptedPayloadModel(io.getlime.security.powerauth.rest.api.model.entity.NonPersonalizedEncryptedPayloadModel) ActivationCreateRequest(io.getlime.security.powerauth.rest.api.model.request.ActivationCreateRequest) PowerAuthPortServiceStub(io.getlime.powerauth.soap.PowerAuthPortServiceStub) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 3 with NonPersonalizedEncryptedPayloadModel

use of io.getlime.security.powerauth.rest.api.model.entity.NonPersonalizedEncryptedPayloadModel in project powerauth-restful-integration by lime-company.

the class PowerAuthNonPersonalizedEncryptor method encrypt.

public ObjectResponse<NonPersonalizedEncryptedPayloadModel> encrypt(byte[] originalData) {
    if (originalData == null) {
        return null;
    }
    NonPersonalizedEncryptedMessage message = encryptor.encrypt(originalData);
    if (message == null) {
        // this will happen only in case of an unlikely randomness error, or if keys are corrupted
        return null;
    }
    NonPersonalizedEncryptedPayloadModel responseObject = new NonPersonalizedEncryptedPayloadModel();
    responseObject.setApplicationKey(BaseEncoding.base64().encode(message.getApplicationKey()));
    responseObject.setEphemeralPublicKey(BaseEncoding.base64().encode(message.getEphemeralPublicKey()));
    responseObject.setSessionIndex(BaseEncoding.base64().encode(message.getSessionIndex()));
    responseObject.setAdHocIndex(BaseEncoding.base64().encode(message.getAdHocIndex()));
    responseObject.setMacIndex(BaseEncoding.base64().encode(message.getMacIndex()));
    responseObject.setNonce(BaseEncoding.base64().encode(message.getNonce()));
    responseObject.setMac(BaseEncoding.base64().encode(message.getMac()));
    responseObject.setEncryptedData(BaseEncoding.base64().encode(message.getEncryptedData()));
    return new ObjectResponse<>(responseObject);
}
Also used : NonPersonalizedEncryptedPayloadModel(io.getlime.security.powerauth.rest.api.model.entity.NonPersonalizedEncryptedPayloadModel) ObjectResponse(io.getlime.core.rest.model.base.response.ObjectResponse) NonPersonalizedEncryptedMessage(io.getlime.security.powerauth.crypto.lib.encryptor.model.NonPersonalizedEncryptedMessage)

Example 4 with NonPersonalizedEncryptedPayloadModel

use of io.getlime.security.powerauth.rest.api.model.entity.NonPersonalizedEncryptedPayloadModel in project powerauth-restful-integration by lime-company.

the class PowerAuthNonPersonalizedEncryptor method decrypt.

public byte[] decrypt(ObjectRequest<NonPersonalizedEncryptedPayloadModel> request) {
    if (request == null) {
        return null;
    }
    NonPersonalizedEncryptedPayloadModel requestObject = request.getRequestObject();
    if (requestObject == null) {
        return null;
    }
    NonPersonalizedEncryptedMessage message = new NonPersonalizedEncryptedMessage();
    message.setApplicationKey(BaseEncoding.base64().decode(requestObject.getApplicationKey()));
    message.setEphemeralPublicKey(BaseEncoding.base64().decode(requestObject.getEphemeralPublicKey()));
    message.setSessionIndex(BaseEncoding.base64().decode(requestObject.getSessionIndex()));
    message.setAdHocIndex(BaseEncoding.base64().decode(requestObject.getAdHocIndex()));
    message.setMacIndex(BaseEncoding.base64().decode(requestObject.getMacIndex()));
    message.setNonce(BaseEncoding.base64().decode(requestObject.getNonce()));
    message.setMac(BaseEncoding.base64().decode(requestObject.getMac()));
    message.setEncryptedData(BaseEncoding.base64().decode(requestObject.getEncryptedData()));
    return encryptor.decrypt(message);
}
Also used : NonPersonalizedEncryptedPayloadModel(io.getlime.security.powerauth.rest.api.model.entity.NonPersonalizedEncryptedPayloadModel) NonPersonalizedEncryptedMessage(io.getlime.security.powerauth.crypto.lib.encryptor.model.NonPersonalizedEncryptedMessage)

Aggregations

NonPersonalizedEncryptedPayloadModel (io.getlime.security.powerauth.rest.api.model.entity.NonPersonalizedEncryptedPayloadModel)4 NonPersonalizedEncryptedMessage (io.getlime.security.powerauth.crypto.lib.encryptor.model.NonPersonalizedEncryptedMessage)2 PowerAuthNonPersonalizedEncryptor (io.getlime.security.powerauth.rest.api.base.encryption.PowerAuthNonPersonalizedEncryptor)2 PowerAuthActivationException (io.getlime.security.powerauth.rest.api.base.exception.PowerAuthActivationException)2 ActivationCreateCustomRequest (io.getlime.security.powerauth.rest.api.model.request.ActivationCreateCustomRequest)2 ActivationCreateRequest (io.getlime.security.powerauth.rest.api.model.request.ActivationCreateRequest)2 ActivationCreateResponse (io.getlime.security.powerauth.rest.api.model.response.ActivationCreateResponse)2 IOException (java.io.IOException)2 ObjectResponse (io.getlime.core.rest.model.base.response.ObjectResponse)1 CreateActivationResponse (io.getlime.powerauth.soap.CreateActivationResponse)1 PowerAuthPortServiceStub (io.getlime.powerauth.soap.PowerAuthPortServiceStub)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1