Skip to content

Commit

Permalink
[server] Always return new token in web_get_access_token() RPC.
Browse files Browse the repository at this point in the history
  • Loading branch information
killing committed May 11, 2015
1 parent 14b610a commit e65b2fb
Showing 1 changed file with 14 additions and 61 deletions.
75 changes: 14 additions & 61 deletions server/web-accesstoken-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

struct WebATPriv {
GHashTable *access_token_hash; /* token -> access info */
GHashTable *access_info_hash; /* access info -> token */
pthread_mutex_t lock;

gboolean cluster_mode;
Expand All @@ -38,11 +37,6 @@ typedef struct {
gboolean use_onetime;
} AccessInfo;

typedef struct {
char token[TOKEN_LEN + 1];
long expire_time;
} AccessToken;

static void
free_access_info (AccessInfo *info)
{
Expand All @@ -67,8 +61,6 @@ seaf_web_at_manager_new (SeafileSession *session)
mgr->priv->access_token_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free,
(GDestroyNotify)free_access_info);
mgr->priv->access_info_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, g_free);
pthread_mutex_init (&mgr->priv->lock, NULL);

return mgr;
Expand All @@ -87,19 +79,6 @@ remove_expire_info (gpointer key, gpointer value, gpointer user_data)
return FALSE;
}

static gboolean
remove_expire_token (gpointer key, gpointer value, gpointer user_data)
{
AccessToken *token = (AccessToken *)value;
long now = *((long*)user_data);

if (token && now >= token->expire_time) {
return TRUE;
}

return FALSE;
}

static int
clean_pulse (void *vmanager)
{
Expand All @@ -110,8 +89,6 @@ clean_pulse (void *vmanager)

g_hash_table_foreach_remove (manager->priv->access_token_hash,
remove_expire_info, &now);
g_hash_table_foreach_remove (manager->priv->access_info_hash,
remove_expire_token, &now);

pthread_mutex_unlock (&manager->priv->lock);

Expand Down Expand Up @@ -153,7 +130,6 @@ seaf_web_at_manager_get_access_token (SeafWebAccessTokenManager *mgr,
int use_onetime)
{
GString *key;
AccessToken *token;
AccessInfo *info;
long now = (long)time(NULL);
long expire;
Expand All @@ -170,44 +146,26 @@ seaf_web_at_manager_get_access_token (SeafWebAccessTokenManager *mgr,
strcmp(op, "update-blks-aj") != 0)
return NULL;

key = g_string_new (NULL);
g_string_printf (key, "%s %s %s %s", repo_id, obj_id, op, username);

pthread_mutex_lock (&mgr->priv->lock);

token = g_hash_table_lookup (mgr->priv->access_info_hash, key->str);
/* To avoid returning an almost expired token, we returns token
* that has at least 1 minute "life time".
*/
if (!token || token->expire_time - now <= 60) {
t = gen_new_token (mgr->priv->access_token_hash);
expire = now + TOKEN_EXPIRE_TIME;

token = g_new0 (AccessToken, 1);
memcpy (token->token, t, TOKEN_LEN);
token->expire_time = expire;

g_hash_table_insert (mgr->priv->access_info_hash, g_strdup(key->str), token);

info = g_new0 (AccessInfo, 1);
info->repo_id = g_strdup (repo_id);
info->obj_id = g_strdup (obj_id);
info->op = g_strdup (op);
info->username = g_strdup (username);
info->expire_time = expire;
if (use_onetime) {
info->use_onetime = TRUE;
}

g_hash_table_insert (mgr->priv->access_token_hash, g_strdup(t), info);

g_free (t);
t = gen_new_token (mgr->priv->access_token_hash);
expire = now + TOKEN_EXPIRE_TIME;

info = g_new0 (AccessInfo, 1);
info->repo_id = g_strdup (repo_id);
info->obj_id = g_strdup (obj_id);
info->op = g_strdup (op);
info->username = g_strdup (username);
info->expire_time = expire;
if (use_onetime) {
info->use_onetime = TRUE;
}

g_hash_table_insert (mgr->priv->access_token_hash, g_strdup(t), info);

pthread_mutex_unlock (&mgr->priv->lock);

g_string_free (key, TRUE);
return g_strdup(token->token);
return t;
}

SeafileWebAccess *
Expand Down Expand Up @@ -237,12 +195,7 @@ seaf_web_at_manager_query_access_token (SeafWebAccessTokenManager *mgr,

if (info->use_onetime) {
pthread_mutex_lock (&mgr->priv->lock);
char *key = g_strdup_printf ("%s %s %s %s",
info->repo_id, info->obj_id,
info->op, info->username);
g_hash_table_remove (mgr->priv->access_info_hash, key);
g_hash_table_remove (mgr->priv->access_token_hash, token);
g_free (key);
pthread_mutex_unlock (&mgr->priv->lock);
}

Expand Down

0 comments on commit e65b2fb

Please sign in to comment.