Projekt

Obecné

Profil

« Předchozí | Další » 

Revize d5b85831

Přidáno uživatelem Jiri Trefil před téměř 2 roky(ů)

#10517 repository a controller + service vytvořena.

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java
23 23
    @PostMapping(value="/upload_configuration")
24 24
    public ResponseEntity<String> test(@RequestBody UserConfiguration userConfiguration) {
25 25
        ResponseEntity<String> response = configurationService.addConfiguration(userConfiguration);
26

  
27
        return null;
26
        return response;
28 27
    }
29 28

  
30 29
    @GetMapping(value="/get_configuration")
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationServiceImplementation.java
3 3
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.*;
4 4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.ConfigRepository;
5 5
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserConfigurationJoinRepository;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserRepository;
7 6
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.Crypto;
7
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder;
8 8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.http.HttpStatus;
9 10
import org.springframework.http.ResponseEntity;
10 11
import org.springframework.stereotype.Service;
11 12

  
13
import java.util.HashMap;
12 14
import java.util.List;
15
import java.util.Map;
13 16

  
14 17
@Service
15 18
public class ConfigurationServiceImplementation implements ConfigurationService{
16

  
19
    //repository which represents connection to database and the configuration table in particular
17 20
    @Autowired
18 21
    private ConfigRepository configurationRepository;
22
    //repository for Join table, necessary to add associations between users and configurations
19 23
    @Autowired
20 24
    private UserConfigurationJoinRepository userConfigurationJoinRepository;
21

  
25
    //user service is also necessary for retrieving information about users (primarily database query for fetching id)
22 26
    @Autowired
23 27
    private UserService userService;
24 28
    /**
......
42 46
        User user = cfg.getUser();
43 47
        Configuration configuration = cfg.getConfiguration();
44 48

  
49
        Map<String,Object> json = new HashMap<>();
45 50
        String userName = user.getName();
51
        //fetch the user from db because user in UserConfiguration does not contain id
46 52
        user = this.userService.getUserByName(userName);
47 53
        String configurationDefinition = configuration.getConfig();
54
        //if the request is missing the configuration definition then we kill it
48 55
        if(configurationDefinition == null){
49
            //todo konfigurace neni poslana, chyba requestu
50

  
56
            json.put("message","no configuration definition provided.");
57
            String jsonString = JSONBuilder.buildJSON(json);
58
            return new ResponseEntity<>(jsonString,HttpStatus.BAD_REQUEST);
51 59
        }
60
        //create the hash of the configuration (w/o salting)
52 61
        String configHash = Crypto.hashString(configurationDefinition);
53 62
        Configuration existingConfiguration = this.configurationRepository.findConfigurationByConfigHash(configHash);
54 63
        //configuration definition does not exist => upload the configuration into database
......
56 65
            configuration.setHash(configHash);
57 66
            //save the configuration itself
58 67
            Configuration tmp = this.configurationRepository.save(configuration);
68
            //can only happen if db server fails or a constraint is breached
59 69
            if(tmp == null){
60
                //todo selhal insert do databaze
70
                json.put("message","fatal server failure");
71
                return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.INTERNAL_SERVER_ERROR);
61 72
            }
62 73
        }
63

  
64 74
        //pair the configuration to the user
65
        return pairConfigurationWithUser(user,configuration);
75
        pairConfigurationWithUser(user,configuration);
76
        json.put("message","configuration uploaded successfully");
77
        return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.OK);
66 78

  
67 79

  
68 80
    }
69 81

  
82
    /**
83
     * This method saves user and configuration id into join table
84
     * creates association between user and configuration in the sense of: "User @param user owns configuration @param configuration"
85
     * (Multiple users can own the same configuration but the configuration is not public)
86
     * @param user User - user who will be associated with configuration @param configuration.
87
     * @param configuration Configuration - the configuration that will be associated with user (just the id is necessary)
88
     * @return ResponseEntity<String> - Http response with status code and message about the operation
89
     */
70 90
    @Override
71 91
    public ResponseEntity<String> pairConfigurationWithUser(User user, Configuration configuration) {
92
        Map<String,Object> json = new HashMap<>();
72 93
        final UserConfigKey key = new UserConfigKey(user.getId(),configuration.getId());
73 94
        boolean exists = this.userConfigurationJoinRepository.existsById(key);
95
        //the configuration pairing already exists, we do not have to do anything
96
        //request like this should not happen from client, something fishy might be going on
97
        //or the request is a duplicate
74 98
        if(exists){
75
            //todo uzivatel se chce sparovat s konfiguraci se kterou je jiz sparovan, jenom poslat ok nebo error
99
            json.put("message","configuration already exists in your collection!");
100
            return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.BAD_REQUEST);
76 101
        }
77 102
        //save the relation between user and configuration
78 103
        this.userConfigurationJoinRepository.save(new UserConfigurationJoin(key));
79
        return null;
104
        json.put("message","configuration added to collection.");
105
        return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.OK);
80 106
    }
81 107

  
82 108
    @Override
......
87 113
        //fetch all configurations this particular user can see
88 114
        //ie all public configs + configurations uploaded by this particular user
89 115
        List<Configuration> configurations = this.configurationRepository.getAllUserConfigurations(userInfo.getId());
90
        return null;
116
        StringBuilder sb = new StringBuilder();
117
        for(int i = 0; i < configurations.size(); i++){
118
            sb.append(configurations.get(i).getConfig());
119
        }
120
        Map<String,Object> json = new HashMap<>();
121
        json.put("message","configuration retrived");
122
        json.put("configurations",sb.toString());
123
        String jsonString = JSONBuilder.buildJSON(json);
124
        return new ResponseEntity<>(jsonString,HttpStatus.OK);
91 125
    }
92 126
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/Crypto.java
4 4
import java.nio.charset.StandardCharsets;
5 5
import java.security.MessageDigest;
6 6
import java.security.NoSuchAlgorithmException;
7

  
7
import java.util.Random;
8
//TODO should probably be replaced by some third party encryption lib in the future
8 9
public class Crypto {
9

  
10
    protected static final int SALT_LEN = 32;
11
    protected static final int PASS_LEN = 64;
10 12
    /**
11 13
     * Method to hash password
12 14
     * @param password  password from client
......
28 30
        return (new HexBinaryAdapter()).marshal(tmp);
29 31
    }
30 32

  
33
    /**
34
     * Hash string and add salt to the end
35
     * prevention against rainbow table attacks
36
     * @param password String to be hashed
37
     * @return String salted hash of the input
38
     */
39
    public static String hashStringSalt(String password){
40
        String hash = hashString(password);
41
        Random r = new Random();
42
        StringBuffer sb = new StringBuffer();
43
        while(sb.length() < SALT_LEN){
44
            sb.append(Integer.toHexString(r.nextInt()));
45
        }
46
        String salt = sb.substring(0,SALT_LEN);
47
        hash += salt;
48
        return hash;
49
    }
50

  
51

  
52

  
31 53
    /**
32 54
     * Method compares user password with stored hash
33 55
     * @param password String user provided password
......
39 61
        return hash.equals(passwordHash);
40 62
    }
41 63

  
64
    /**
65
     * Method compares user password with stored hash with salt
66
     * @param password String user provided password
67
     * @param hash String hash saved in database
68
     * @return true if hashes are the same
69
     */
70
    public static boolean compareHashesSalt(String password, String hash){
71
        //toss the salt aside
72
        String strippedHash = hash.substring(0,PASS_LEN);
73
        return compareHashes(password,strippedHash);
74
    }
75

  
42 76

  
43 77
}

Také k dispozici: Unified diff