Commit ed5dc629a2c44f0b086995bf91a0a2a749879bc9
1 parent
de714f8a
add username to user class. This will contain the common name (cn) of an ldap au…
…thenticated user and the email address of an otherwise authenticated user. This is done to be able to have a good way to store an application user for ldap authenticated users.
Showing
6 changed files
with
125 additions
and
74 deletions
... | ... | @@ -18,19 +18,20 @@ function Session(sInfo, sId, sUser) |
18 | 18 | |
19 | 19 | Session.prototype.loadUserJSON = function(data) |
20 | 20 | { |
21 | + this.username = data.username; | |
21 | 22 | this.email = data.email; |
22 | 23 | this.firstname = data.firstname; |
23 | 24 | this.surname = data.surname; |
24 | 25 | |
25 | 26 | name = ""; |
26 | - if ('' == this.email) { | |
27 | + if ('' == this.username) { | |
27 | 28 | name = "not logged in"; |
28 | 29 | $("li.signup").removeClass("hide"); |
29 | 30 | $("li.login").removeClass("hide"); |
30 | 31 | $("li.logout").addClass("hide"); |
31 | 32 | } else { |
32 | 33 | if ('' == this.firstname || '' == this.surname) { |
33 | - name += this.email; | |
34 | + name += this.username; | |
34 | 35 | } else { |
35 | 36 | name += this.firstname + " " + this.surname; |
36 | 37 | } | ... | ... |
... | ... | @@ -27,15 +27,27 @@ |
27 | 27 | |
28 | 28 | #include "class.h" |
29 | 29 | #include "uuid.h" |
30 | +#include "auth.h" | |
30 | 31 | #include "storage/storage.h" |
31 | 32 | |
32 | 33 | CLASS(User) { |
33 | - unsigned long hash; | |
34 | - | |
34 | + unsigned long hash; | |
35 | + AuthModule auth_type; | |
36 | + | |
37 | + /** | |
38 | + * username holds the identifier of the user. | |
39 | + * For ldap users this is the common name. | |
40 | + * For registered users this is their email | |
41 | + * address. | |
42 | + * The UUID of the user is created from this | |
43 | + * username. | |
44 | + */ | |
45 | + char * username; | |
35 | 46 | char * email; |
36 | 47 | char * firstname; |
37 | 48 | char * surname; |
38 | 49 | |
50 | + size_t * nusername; | |
39 | 51 | size_t * nemail; |
40 | 52 | size_t * nfirstname; |
41 | 53 | size_t * nsurname; | ... | ... |
... | ... | @@ -67,6 +67,7 @@ _controllerProcessUserCreateArgs(Hash args, User * user, Credential * cred) |
67 | 67 | |
68 | 68 | *user = new(User, |
69 | 69 | (char *)(email->value), email->nvalue, |
70 | + (char *)(email->value), email->nvalue, | |
70 | 71 | (char *)(firstname->value), firstname->nvalue, |
71 | 72 | (char *)(surname->value), surname->nvalue); |
72 | 73 | ... | ... |
... | ... | @@ -34,7 +34,7 @@ |
34 | 34 | |
35 | 35 | |
36 | 36 | #define USER_JSON \ |
37 | - "{\"email\":\"%s\",\"firstname\":\"%s\",\"surname\":\"%s\"}" | |
37 | + "{\"username\":\"%s\",\"email\":\"%s\",\"firstname\":\"%s\",\"surname\":\"%s\"}" | |
38 | 38 | |
39 | 39 | char * |
40 | 40 | controllerCurrentuserRead(Application app, Session sess, Hash args) |
... | ... | @@ -43,11 +43,13 @@ controllerCurrentuserRead(Application app, Session sess, Hash args) |
43 | 43 | size_t nbuffer; |
44 | 44 | |
45 | 45 | nbuffer = snprintf(NULL, 0, USER_JSON, |
46 | + (NULL != sess->user)? sess->user->username : "", | |
46 | 47 | (NULL != sess->user)? sess->user->email : "", |
47 | 48 | (NULL != sess->user)? sess->user->firstname : "", |
48 | 49 | (NULL != sess->user)? sess->user->surname : ""); |
49 | 50 | buffer = memMalloc(nbuffer); |
50 | 51 | nbuffer = sprintf(buffer, USER_JSON, |
52 | + (NULL != sess->user)? sess->user->username : "", | |
51 | 53 | (NULL != sess->user)? sess->user->email : "", |
52 | 54 | (NULL != sess->user)? sess->user->firstname : "", |
53 | 55 | (NULL != sess->user)? sess->user->surname : ""); | ... | ... |
... | ... | @@ -44,64 +44,86 @@ applicationLogin( |
44 | 44 | Credential credential, |
45 | 45 | Session session) |
46 | 46 | { |
47 | - size_t i; | |
48 | - Uuid search; | |
49 | - int authenticated = 0; | |
47 | + Uuid search; | |
48 | + AuthModule auth_module; | |
50 | 49 | |
51 | - User user = new(User, NULL); | |
50 | + User user = new(User, NULL); | |
52 | 51 | |
53 | - user->email = CRED_PWD(credential).user; | |
54 | - user->nemail = &CRED_PWD(credential).nuser; | |
52 | + user->username = CRED_PWD(credential).user; | |
53 | + user->nusername = &CRED_PWD(credential).nuser; | |
55 | 54 | search = indexUuid(user, this->user_namespace); |
56 | 55 | |
57 | - for (i=0; i<this->nauth; i++) { | |
58 | - if (authenticate(this->auth[i], credential, search)) { | |
59 | - session->user = user; | |
60 | - | |
61 | - switch (credential->type) { | |
62 | - case CRED_PASSWORD: | |
63 | - { | |
64 | - char * user_serialized; | |
65 | - size_t nuser_serialized; | |
66 | - | |
67 | - storageGet( | |
68 | - this->users, | |
69 | - (char *)(search->uuid).value, | |
70 | - sizeof((search->uuid).value), | |
71 | - &user_serialized, | |
72 | - &nuser_serialized); | |
73 | - | |
74 | - if (NULL != user_serialized) { | |
75 | - unserialize( | |
76 | - session->user, | |
77 | - (unsigned char *)user_serialized, | |
78 | - nuser_serialized); | |
79 | - MEM_FREE(user_serialized); | |
80 | - } else { | |
81 | - // this is a user authenticated via another method | |
82 | - // than the password database and has not yet set | |
83 | - // additional user informations. | |
84 | - session->user = NULL; | |
85 | - delete(session->user); | |
86 | - session->user = new(User, | |
87 | - CRED_PWD(credential).user, | |
88 | - CRED_PWD(credential).nuser, | |
89 | - CSTRA(""), | |
90 | - CSTRA("")); | |
91 | - } | |
92 | - } | |
93 | - break; | |
94 | - | |
95 | - default: | |
96 | - break; | |
97 | - } | |
98 | - | |
99 | - authenticated = 1; | |
100 | - break; | |
56 | + auth_module = authenticate(this->auth, credential, search); | |
57 | + | |
58 | + if (0 != auth_module) { | |
59 | + char * user_serialized; | |
60 | + size_t nuser_serialized; | |
61 | + | |
62 | + session->user = user; | |
63 | + | |
64 | + switch (credential->type) { | |
65 | + case CRED_PASSWORD: | |
66 | + storageGet( | |
67 | + this->users, | |
68 | + (char *)(search->uuid).value, | |
69 | + sizeof((search->uuid).value), | |
70 | + &user_serialized, | |
71 | + &nuser_serialized); | |
72 | + | |
73 | + if (NULL != user_serialized) { | |
74 | + unserialize( | |
75 | + session->user, | |
76 | + (unsigned char *)user_serialized, | |
77 | + nuser_serialized); | |
78 | + MEM_FREE(user_serialized); | |
79 | + } else { | |
80 | + /** | |
81 | + * this is a user authenticated via another method | |
82 | + * than the password database and has not yet | |
83 | + * logged in. | |
84 | + * NOTE: first we have to remove the search user and | |
85 | + * as username is initialized with something that we | |
86 | + * will free later here we must set it to NULL so that | |
87 | + * the delete will not free it. | |
88 | + */ | |
89 | + session->user->username = NULL; | |
90 | + delete(session->user); | |
91 | + session->user = new(User, | |
92 | + CRED_PWD(credential).user, | |
93 | + CRED_PWD(credential).nuser, | |
94 | + CSTRA(""), | |
95 | + CSTRA(""), | |
96 | + CSTRA("")); | |
97 | + | |
98 | + serialize( | |
99 | + session->user, | |
100 | + (unsigned char **)&user_serialized, | |
101 | + &nuser_serialized); | |
102 | + /** | |
103 | + * \todo | |
104 | + * Handle error...if this fails we have most likely | |
105 | + * a collision. | |
106 | + */ | |
107 | + storagePut( | |
108 | + this->users, | |
109 | + (char *)(search->uuid).value, | |
110 | + sizeof((search->uuid).value), | |
111 | + user_serialized, | |
112 | + nuser_serialized); | |
113 | + MEM_FREE(user_serialized); | |
114 | + } | |
115 | + | |
116 | + session->user->auth_type = auth_module; | |
117 | + break; | |
118 | + | |
119 | + default: | |
120 | + break; | |
101 | 121 | } |
122 | + | |
123 | + return TRUE; | |
102 | 124 | } |
103 | 125 | |
104 | - return authenticated; | |
126 | + return FALSE; | |
105 | 127 | } |
106 | 128 | |
107 | 129 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -34,10 +34,12 @@ static |
34 | 34 | int |
35 | 35 | userCtor(void * _this, va_list * params) |
36 | 36 | { |
37 | - User this = _this; | |
38 | - char * email = va_arg(* params, char *); | |
37 | + User this = _this; | |
38 | + char * username = va_arg(* params, char *); | |
39 | 39 | |
40 | - if (NULL != email) { | |
40 | + if (NULL != username) { | |
41 | + size_t nusername = va_arg(* params, size_t); | |
42 | + char * email = va_arg(* params, char *); | |
41 | 43 | size_t nemail = va_arg(* params, size_t); |
42 | 44 | char * firstname = va_arg(* params, char *); |
43 | 45 | size_t nfirstname = va_arg(* params, size_t); |
... | ... | @@ -45,12 +47,17 @@ userCtor(void * _this, va_list * params) |
45 | 47 | size_t nsurname = va_arg(* params, size_t); |
46 | 48 | |
47 | 49 | size_t storage_size = |
50 | + nusername + 1 + | |
48 | 51 | nemail + 1 + |
49 | 52 | nfirstname + 1 + |
50 | 53 | nsurname + 1 + |
51 | - 3 * sizeof(size_t); | |
54 | + 4 * sizeof(size_t); | |
52 | 55 | |
53 | - this->email = memMalloc(storage_size); | |
56 | + this->username = memMalloc(storage_size); | |
57 | + memcpy(this->username, username, nusername); | |
58 | + this->username[nusername] = '\0'; | |
59 | + | |
60 | + this->email = this->username + nusername + 1; | |
54 | 61 | memcpy(this->email, email, nemail); |
55 | 62 | this->email[nemail] = '\0'; |
56 | 63 | |
... | ... | @@ -62,7 +69,10 @@ userCtor(void * _this, va_list * params) |
62 | 69 | memcpy(this->surname, surname, nsurname); |
63 | 70 | this->surname[nsurname] = '\0'; |
64 | 71 | |
65 | - this->nemail = (size_t *)(this->surname + nsurname + 1); | |
72 | + this->nusername = (size_t *)(this->surname + nsurname + 1); | |
73 | + *this->nusername = nusername; | |
74 | + | |
75 | + this->nemail = this->nusername + 1; | |
66 | 76 | *this->nemail = nemail; |
67 | 77 | |
68 | 78 | this->nfirstname = this->nemail + 1; |
... | ... | @@ -81,8 +91,8 @@ userDtor(void * _this) |
81 | 91 | { |
82 | 92 | User this = _this; |
83 | 93 | |
84 | - if (NULL != this->email) { | |
85 | - MEM_FREE(this->email); | |
94 | + if (NULL != this->username) { | |
95 | + MEM_FREE(this->username); | |
86 | 96 | } |
87 | 97 | } |
88 | 98 | |
... | ... | @@ -96,14 +106,15 @@ userSerialize( |
96 | 106 | User this = _this; |
97 | 107 | |
98 | 108 | *nserialized = |
109 | + *this->nusername + 1 + | |
99 | 110 | *this->nemail + 1 + |
100 | 111 | *this->nfirstname + 1 + |
101 | 112 | *this->nsurname + 1 + |
102 | - 3 * sizeof(size_t); | |
113 | + 4 * sizeof(size_t); | |
103 | 114 | |
104 | 115 | *serialized = memMalloc(*nserialized); |
105 | 116 | |
106 | - memcpy(*serialized, this->email, *nserialized); | |
117 | + memcpy(*serialized, this->username, *nserialized); | |
107 | 118 | } |
108 | 119 | |
109 | 120 | static |
... | ... | @@ -116,16 +127,18 @@ userUnserialize( |
116 | 127 | User this = _this; |
117 | 128 | size_t * user_data_sizes; |
118 | 129 | |
119 | - this->email = memMalloc(nserialized); | |
120 | - memcpy(this->email, serialized, nserialized); | |
130 | + this->username = memMalloc(nserialized); | |
131 | + memcpy(this->username, serialized, nserialized); | |
121 | 132 | |
122 | 133 | user_data_sizes = |
123 | - (size_t *)(this->email + nserialized - 3 * sizeof(size_t)); | |
134 | + (size_t *)(this->username + nserialized - 4 * sizeof(size_t)); | |
124 | 135 | |
125 | - this->nemail = user_data_sizes; | |
126 | - this->nfirstname = user_data_sizes + 1; | |
127 | - this->nsurname = user_data_sizes + 2; | |
136 | + this->nusername = user_data_sizes; | |
137 | + this->nemail = user_data_sizes + 1; | |
138 | + this->nfirstname = user_data_sizes + 2; | |
139 | + this->nsurname = user_data_sizes + 3; | |
128 | 140 | |
141 | + this->email = this->username + *this->nusername + 1; | |
129 | 142 | this->firstname = this->email + *this->nemail + 1; |
130 | 143 | this->surname = this->firstname + *this->nfirstname + 1; |
131 | 144 | } |
... | ... | @@ -137,8 +150,8 @@ userIndexUuid(void * _this, Uuid namespace) |
137 | 150 | User this = _this; |
138 | 151 | |
139 | 152 | return uuidVersion3( |
140 | - (unsigned char *)this->email, | |
141 | - *this->nemail, | |
153 | + (unsigned char *)this->username, | |
154 | + *this->nusername, | |
142 | 155 | namespace); |
143 | 156 | } |
144 | 157 | ... | ... |
Please
register
or
login
to post a comment