source: trunk/grails-app/services/api/ApiService.groovy @ 2184

Last change on this file since 2184 was 2184, checked in by work@…, 9 years ago

fixed getSubjectsForStudy api method

File size: 1.4 KB
Line 
1/**
2 * ApiService Service
3 *
4 * Description of my service
5 *
6 * @author  your email (+name?)
7 * @since       2010mmdd
8 * @package     ???
9 *
10 * Revision information:
11 * $Rev: 1430 $
12 * $Author: work@osx.eu $
13 * $Date: 2011-01-21 21:05:36 +0100 (Fri, 21 Jan 2011) $
14 */
15package api
16
17import java.security.MessageDigest
18
19class ApiService {
20    // the shared secret used to validate api calls
21    static final String API_SECRET = "th!s_sH0uld^Pr0bab7y_m0v3_t%_th3_uSeR_d0Ma!n_ins7ead!"
22    static transactional = true
23
24    /**
25     * validate a client request by checking the validation checksum
26     * @param deviceID
27     * @param validation
28     * @return
29     */
30    def validateRequest(String deviceID, String validation) {
31        return true
32
33        // get token for this device ID
34        Token token = Token.findByDeviceID(deviceID)
35
36        // increase sequence
37        if (token) {
38            token.sequence = token.sequence+1
39            token.save()
40
41            // generate the validation checksum
42            MessageDigest digest = MessageDigest.getInstance("MD5")
43            String validationSum = new BigInteger(1,digest.digest("${token.deviceToken}${token.sequence}${API_SECRET}".getBytes())).toString(16).padLeft(32,"0")
44
45            // check if the validation confirms
46            return (validation == validationSum)
47        } else {
48            // no such token, re-authenticate
49            return false
50        }
51    }
52}
Note: See TracBrowser for help on using the repository browser.