1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-06-28 16:06:49 +00:00
seafile-server/server/web-accesstoken-mgr.c

248 lines
6.8 KiB
C
Raw Normal View History

2016-08-10 06:53:33 +00:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include "common.h"
#include <timer.h>
2016-08-10 06:53:33 +00:00
#include <pthread.h>
#include "seafile-session.h"
#include "web-accesstoken-mgr.h"
#include "seafile-error.h"
#include "utils.h"
#include "log.h"
#define CLEANING_INTERVAL_MSEC 1000*300 /* 5 minutes */
#define TOKEN_EXPIRE_TIME 3600 /* 1 hour */
#define TOKEN_LEN 36
struct WebATPriv {
GHashTable *access_token_hash; /* token -> access info */
pthread_mutex_t lock;
gboolean cluster_mode;
struct ObjCache *cache;
};
typedef struct WebATPriv WebATPriv;
/* #define DEBUG 1 */
typedef struct {
char *repo_id;
char *obj_id;
char *op;
char *username;
long expire_time;
gboolean use_onetime;
} AccessInfo;
static void
free_access_info (AccessInfo *info)
{
if (!info)
return;
g_free (info->repo_id);
g_free (info->obj_id);
g_free (info->op);
g_free (info->username);
g_free (info);
}
SeafWebAccessTokenManager*
seaf_web_at_manager_new (SeafileSession *session)
{
SeafWebAccessTokenManager *mgr = g_new0 (SeafWebAccessTokenManager, 1);
mgr->seaf = session;
mgr->priv = g_new0(WebATPriv, 1);
mgr->priv->access_token_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free,
(GDestroyNotify)free_access_info);
pthread_mutex_init (&mgr->priv->lock, NULL);
return mgr;
}
static gboolean
remove_expire_info (gpointer key, gpointer value, gpointer user_data)
{
AccessInfo *info = (AccessInfo *)value;
long now = *((long*)user_data);
if (info && now >= info->expire_time) {
return TRUE;
}
return FALSE;
}
static int
clean_pulse (void *vmanager)
{
SeafWebAccessTokenManager *manager = vmanager;
long now = (long)time(NULL);
pthread_mutex_lock (&manager->priv->lock);
g_hash_table_foreach_remove (manager->priv->access_token_hash,
remove_expire_info, &now);
pthread_mutex_unlock (&manager->priv->lock);
return TRUE;
}
int
seaf_web_at_manager_start (SeafWebAccessTokenManager *mgr)
{
ccnet_timer_new (clean_pulse, mgr, CLEANING_INTERVAL_MSEC);
return 0;
}
static char *
gen_new_token (GHashTable *token_hash)
{
char uuid[37];
char *token;
while (1) {
gen_uuid_inplace (uuid);
token = g_strndup(uuid, TOKEN_LEN);
/* Make sure the new token doesn't conflict with an existing one. */
if (g_hash_table_lookup (token_hash, token) != NULL)
g_free (token);
else
return token;
}
}
char *
seaf_web_at_manager_get_access_token (SeafWebAccessTokenManager *mgr,
const char *repo_id,
const char *obj_id,
const char *op,
const char *username,
int use_onetime,
GError **error)
{
AccessInfo *info;
long now = (long)time(NULL);
long expire;
char *t;
SeafileWebAccess *webaccess;
if (strcmp(op, "view") != 0 &&
strcmp(op, "download") != 0 &&
strcmp(op, "downloadblks") != 0 &&
strcmp(op, "download-dir") != 0 &&
strcmp(op, "download-multi") != 0 &&
2018-08-10 09:47:19 +00:00
strcmp(op, "download-link") != 0 &&
strcmp(op, "download-dir-link") != 0 &&
strcmp(op, "download-multi-link") != 0 &&
2016-08-10 06:53:33 +00:00
strcmp(op, "upload") != 0 &&
strcmp(op, "update") != 0 &&
2018-08-10 09:47:19 +00:00
strcmp(op, "upload-link") != 0 &&
2016-08-10 06:53:33 +00:00
strcmp(op, "upload-blks-api") != 0 &&
strcmp(op, "upload-blks-aj") != 0 &&
strcmp(op, "update-blks-api") != 0 &&
strcmp(op, "update-blks-aj") != 0) {
g_set_error (error, SEAFILE_DOMAIN, SEAF_ERR_GENERAL,
"Invalid operation type.");
return NULL;
}
pthread_mutex_lock (&mgr->priv->lock);
t = gen_new_token (mgr->priv->access_token_hash);
expire = now + seaf->web_token_expire_time;
2016-08-10 06:53:33 +00:00
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);
#ifdef HAVE_EVHTP
Go fileserver (#437) * Initial commit for fileserver written in golang. [gofileserver] Fix some syntaxt errors. Add fs backend and objstore test (#352) * Add fs backend and objstore test * modify test case and optimize fs backend * Modify function name and first write temporary files * Don't need to reopen the temp files Add comment for objstore (#354) * Add comment for objstore * Modify comment Add commitmgr and test case (#356) * Add commitmgr and test case * Redefine the interface * Modify comment and interface * Modify parameter and del unused method * Add comment for FromData and ToData Add blockmgr and test case (#357) * Add blockmgr and test case * Modify comment and interface Add fsmgr and test case (#358) * Add fsmgr and test case * Add save interface and error details * Modify errors and comments Add searpc package and test case (#360) * Add searpc package * Add searpc test case * Add return error and add Request struct * Modify returned error * Modify comments add checkPerm (#369) Add file and block download (#363) * Add file and block download * Modify init and use aes algorithm * Get block by offset and add stat method * Modify objID's type * Fix reset pos after add start * Add http error handing and record log when failed to read block or write block to response * Modify http return code and value names * Modify http return code and add log info * Block read add comment and only repeat once load ccnetdb and support sqlite (#371) Add zip download (#372) * Add zip download * Modify pack dir and log info * Modify http return code and use Deflate zip compression methods add /repo/<repo-id>/permission-check (#375) add /<repo-id>/commit/HEAD (#377) add /repo/<repo-id>/commit/<id> (#379) add /repo/<repo-id>/block/<id> (#380) add /repo/<repo-id>/fs-id-list (#383) add /repo/head-commits-multi (#388) Add file upload api (#378) * Add file upload api * Upload api implements post multi files and create relative path * Modify handle error and save files directly * Fix rebase conflict * index block use channel and optimize mkdir with parents * Handle jobs and results in a loop * Mkdir with parents use postMultiFiles and use pointer of SeafDirent * Del diff_simple size_sched virtual_repo * Need to check the path with and without slash * Modify merge trees and add merge test case * Del postFile and don't close results channel * Close the file and remove multipart temp file * Modify merge test case and compare the first name of path * Use pointer of Entries for SeafDir * Add test cases for different situations add /repo/<repo-id>/pack-fs (#389) add POST /<repo-id>/check-fs and /<repo-id>/check-blocks (#396) Merge compute repo (#397) * Add update repo size and merge virtual repo * Eliminate lint warnings * Uncomment merge virtual repo and compute repo size * Need init the dents * Use interface{} param and modify removeElems * Move update dir to file.go and modify logs * Del sync pkg add PUT /<repo-id>/commit/<commit-id> (#400) add PUT /<repo-id>/block/<id> (#401) add POST /<repo-id>/recv-fs (#398) add PUT /<repo-id>/commit/HEAD (#402) Add http return code (#403) Add file update API (#399) * Add file update API * Add GetObjIDByPath and fix change size error * Add traffic statistics for update api add diffTrees unit test (#391) add GET /accessible-repos (#406) add GET /<repo-id>/block-map/<file-id> (#405) Add test update repo size and merge virtual repo (#409) * Update dir need update repo size * Add test update repo size and merge virtual repo * Add delay for test ajax * Add delay before get repo size and modify comment Use go fileserver for unit test (#410) * Use go fileserver for unit test * Blocking scheduling update repo size * Add delay because of sqlite doesn't support concurrency * Post use multipart form encode * Del mysql database when test finished * Fix merge virtual repo failed when use sqlite3 Add upload block API (#412) fixed error Add quota-check API (#426) use diff package * Use central conf for go fileserver (#428) * Use central conf for go fileserver * Fix log error * use store id and remove share get repo owner (#430) * Fix permission error (#432) Co-authored-by: feiniks <36756310+feiniks@users.noreply.github.com> Co-authored-by: Xiangyue Cai <caixiangyue007@gmail.com>
2021-01-04 03:41:53 +00:00
if (!seaf->go_fileserver) {
if (strcmp(op, "download-dir") == 0 ||
strcmp(op, "download-multi") == 0 ||
strcmp(op, "download-dir-link") == 0 ||
strcmp(op, "download-multi-link") == 0) {
webaccess = g_object_new (SEAFILE_TYPE_WEB_ACCESS,
"repo_id", info->repo_id,
"obj_id", info->obj_id,
"op", info->op,
"username", info->username,
NULL);
if (zip_download_mgr_start_zip_task (seaf->zip_download_mgr,
t, webaccess, error) < 0) {
pthread_mutex_lock (&mgr->priv->lock);
g_hash_table_remove (mgr->priv->access_token_hash, t);
pthread_mutex_unlock (&mgr->priv->lock);
g_object_unref (webaccess);
g_free (t);
return NULL;
}
2016-08-10 06:53:33 +00:00
g_object_unref (webaccess);
}
}
#endif
2016-08-10 06:53:33 +00:00
return t;
}
SeafileWebAccess *
seaf_web_at_manager_query_access_token (SeafWebAccessTokenManager *mgr,
const char *token)
{
SeafileWebAccess *webaccess;
AccessInfo *info;
pthread_mutex_lock (&mgr->priv->lock);
info = g_hash_table_lookup (mgr->priv->access_token_hash, token);
pthread_mutex_unlock (&mgr->priv->lock);
if (info != NULL) {
long expire_time = info->expire_time;
long now = (long)time(NULL);
if (now - expire_time >= 0) {
return NULL;
} else {
webaccess = g_object_new (SEAFILE_TYPE_WEB_ACCESS,
"repo_id", info->repo_id,
"obj_id", info->obj_id,
"op", info->op,
"username", info->username,
NULL);
if (info->use_onetime) {
pthread_mutex_lock (&mgr->priv->lock);
g_hash_table_remove (mgr->priv->access_token_hash, token);
pthread_mutex_unlock (&mgr->priv->lock);
}
return webaccess;
}
}
return NULL;
}