Skip to content

Commit

Permalink
KAA-244: Trustful verifier implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvayka committed Feb 2, 2015
1 parent af3fb16 commit bbd4efd
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 22 deletions.
@@ -0,0 +1,44 @@
/*
* Copyright 2014 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.common.utils;

import java.nio.charset.Charset;
import java.util.zip.CRC32;

/**
*
* An util class that provides convenient methods to get crc32 checksum from {@link String}
*
* @author Andrew Shvayka
*
*/
public class CRC32Util {

private static final Charset UTF8 = Charset.forName("UTF-8");

/**
* Calculates the crc32 hash based on the name parameter.
*
* @param name
* the name parameter
* @return crc32 hash
*/
public static int crc32(String name) {
CRC32 crc32 = new CRC32();
crc32.update(name.getBytes(UTF8));
return (int) crc32.getValue();
}
}
Expand Up @@ -52,14 +52,29 @@ public interface UserVerifierCallback {
*/
void onInternalError();

/**
* Failed verification due to internal error
*/
void onInternalError(String reason);

/**
* Failed verification due to connection error
*/
void onConnectionError();

/**
* Failed verification due to connection error
*/
void onConnectionError(String reason);

/**
* Failed verification due to remote authentication service error
*/
void onRemoteError();

/**
* Failed verification due to remote authentication service error
*/
void onRemoteError(String reason);

}
@@ -0,0 +1,11 @@
package org.kaaproject.kaa.server.common.verifier;

public enum UserVerifierErrorCode {
NO_VERIFIER_CONFIGURED,
TOKEN_INVALID,
TOKEN_EXPIRED,
INTERNAL_ERROR,
CONNECTION_ERROR,
REMOTE_ERROR,
OTHER
}
Expand Up @@ -58,16 +58,4 @@ public static int crc32(ConnectionInfo connectionInfo) {
crc32.update(getNameFromConnectionInfo(connectionInfo).getBytes(UTF8));
return (int) crc32.getValue();
}

/**
* Calculates the crc32 hash based on the name parameter.
*
* @param name the name parameter
* @return crc32 hash
*/
public static int crc32(String name) {
CRC32 crc32 = new CRC32();
crc32.update(name.getBytes(UTF8));
return (int) crc32.getValue();
}
}
Expand Up @@ -166,7 +166,6 @@ private void addLogAppender(String appenderId) {
LogAppender logAppender = logAppenderService.getApplicationAppender(appenderId);
if (logAppender != null) {
addAppender(appenderId, logAppender);
} else {
LOG.info("[{}] Log appender [{}] registered.", applicationId, appenderId);
}
} else {
Expand Down
@@ -1,17 +1,38 @@
/*
* Copyright 2014 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.operations.service.akka.actors.core;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.kaaproject.kaa.common.dto.user.UserVerifierDto;
import org.kaaproject.kaa.server.common.thrift.gen.operations.Notification;
import org.kaaproject.kaa.server.common.verifier.UserVerifier;
import org.kaaproject.kaa.server.common.verifier.UserVerifierCallback;
import org.kaaproject.kaa.server.common.verifier.UserVerifierContext;
import org.kaaproject.kaa.server.common.verifier.UserVerifierErrorCode;
import org.kaaproject.kaa.server.operations.service.akka.messages.core.user.verification.UserVerificationRequestMessage;
import org.kaaproject.kaa.server.operations.service.akka.messages.core.user.verification.UserVerificationResponseMessage;
import org.kaaproject.kaa.server.operations.service.user.EndpointUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import akka.actor.ActorRef;

public class ApplicationUserVerifierActorMessageProcessor {

private static final Logger LOG = LoggerFactory.getLogger(ApplicationUserVerifierActorMessageProcessor.class);
Expand All @@ -34,7 +55,8 @@ private void initUserVerifiers() {
for (UserVerifierDto dto : endpointUserService.findUserVerifiers(applicationId)) {
try {
LOG.trace("Initializing user verifier for {}", dto);
userVerifiers.put(dto.getVerifierId(), createUserVerifier(dto));
UserVerifier verifier = createUserVerifier(dto);
userVerifiers.put(dto.getVerifierId(), verifier);
} catch (Exception e) {
LOG.error("Failed to create user verifier", e);
}
Expand All @@ -60,22 +82,133 @@ private UserVerifier createUserVerifier(UserVerifierDto verifierDto) throws Refl
throw e;
}
}

public void verifyUser(UserVerificationRequestMessage message) {
// TODO Auto-generated method stub
UserVerifier verifier = userVerifiers.get(message.getVerifierId());
if (verifier != null) {

} else {
message.getOriginator().tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.NO_VERIFIER_CONFIGURED),
ActorRef.noSender());
}
}

public void processNotification(Notification notification) {
// TODO Auto-generated method stub
LOG.debug("Process user verifier notification [{}]", notification);
int verifierId = notification.getUserVerifierId();
switch (notification.getOp()) {
case ADD_USER_VERIFIER:
addUserVerifier(verifierId);
break;
case REMOVE_USER_VERIFIER:
removeUserVerifier(verifierId);
break;
case UPDATE_USER_VERIFIER:
removeUserVerifier(verifierId);
addUserVerifier(verifierId);
break;
default:
LOG.debug("[{}][{}] Operation [{}] is not supported.", applicationId, verifierId, notification.getOp());
}
}

private void addUserVerifier(int verifierId) {
LOG.info("[{}] Adding user verifier with id [{}].", applicationId, verifierId);
if (!userVerifiers.containsKey(verifierId)) {
UserVerifierDto verifierDto = endpointUserService.findUserVerifier(applicationId, verifierId);
if (verifierDto != null) {
try {
userVerifiers.put(verifierId, createUserVerifier(verifierDto));
LOG.info("[{}] user verifier [{}] registered.", applicationId, verifierId);
} catch (Exception e) {
LOG.error("Failed to create user verifier", e);
}
}
} else {
LOG.info("[{}] User verifier [{}] is already registered.", applicationId, verifierId);
}
}

private void removeUserVerifier(int appenderId) {
if (userVerifiers.containsKey(appenderId)) {
LOG.info("[{}] Stopping user verifier with id [{}].", applicationId, appenderId);
userVerifiers.remove(appenderId).stop();
} else {
LOG.warn("[{}] Can't remove unregistered user verifier with id [{}]", applicationId, appenderId);
}
}

void preStart() {
};

void postStop() {
for (UserVerifier verifier : userVerifiers.values()) {
verifier.stop();
for (Entry<Integer, UserVerifier> verifier : userVerifiers.entrySet()) {
LOG.info("[{}] Stopping user verifier with id [{}].", applicationId, verifier.getKey());
verifier.getValue().stop();
}
}

public static class DefaultVerifierCallback implements UserVerifierCallback {

private final ActorRef endpointActor;

public DefaultVerifierCallback(ActorRef endpointActor) {
super();
this.endpointActor = endpointActor;
}

private void tell(UserVerificationResponseMessage msg){
endpointActor.tell(msg, ActorRef.noSender());
}

@Override
public void onSuccess() {
tell(UserVerificationResponseMessage.success());
}

@Override
public void onTokenInvalid() {
tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.TOKEN_INVALID));
}

@Override
public void onTokenExpired() {
tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.TOKEN_EXPIRED));
}

@Override
public void onVerificationFailure(String reason) {
tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.OTHER, reason));
}

@Override
public void onInternalError() {
tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.INTERNAL_ERROR));
}

@Override
public void onInternalError(String reason) {
tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.TOKEN_EXPIRED, reason));
}

@Override
public void onConnectionError() {
tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.CONNECTION_ERROR));
}

@Override
public void onConnectionError(String reason) {
tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.CONNECTION_ERROR, reason));
}

@Override
public void onRemoteError() {
tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.REMOTE_ERROR));
}

@Override
public void onRemoteError(String reason) {
tell(UserVerificationResponseMessage.failure(UserVerifierErrorCode.REMOTE_ERROR, reason));
}
}
}
@@ -1,5 +1,63 @@
/*
* Copyright 2014 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.operations.service.akka.messages.core.user.verification;

import akka.actor.ActorRef;

public class UserVerificationRequestMessage {

private final ActorRef originator;
private final int verifierId;
private final String userId;
private final String accessToken;

public UserVerificationRequestMessage(ActorRef originator, int verifierId, String userId, String accessToken) {
super();
this.originator = originator;
this.verifierId = verifierId;
this.userId = userId;
this.accessToken = accessToken;
}

public int getVerifierId() {
return verifierId;
}

public String getUserId() {
return userId;
}

public String getAccessToken() {
return accessToken;
}

public ActorRef getOriginator() {
return originator;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("UserVerificationRequestMessage [verifierId=");
builder.append(verifierId);
builder.append(", userId=");
builder.append(userId);
builder.append(", accessToken=");
builder.append(accessToken);
builder.append("]");
return builder.toString();
}
}

0 comments on commit bbd4efd

Please sign in to comment.