Move tokens from sessions to requests
[src/app-framework-binder.git] / src / afb-session.c
index 1bdb59d..3e267fd 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "afb-session.h"
 #include "afb-hook.h"
+#include "afb-token.h"
 #include "verbose.h"
 #include "pearson.h"
 #include "uuid.h"
@@ -60,15 +61,14 @@ struct afb_session
        struct afb_session *next; /**< link to the next */
        unsigned refcount;      /**< count of reference to the session */
        int timeout;            /**< timeout of the session */
-       time_t expiration;      /**< expiration time of the token */
+       time_t expiration;      /**< expiration time of the session */
        pthread_mutex_t mutex;  /**< mutex of the session */
        struct cookie *cookies[COOKIECOUNT]; /**< cookies of the session */
        char *lang;             /**< current language setting for the session */
        uint8_t closed: 1;      /**< is the session closed ? */
        uint8_t autoclose: 1;   /**< close the session when unreferenced */
        uint8_t notinset: 1;    /**< session removed from the set of sessions */
-       uuid_stringz_t uuid;    /**< long term authentication of remote client */
-       uuid_stringz_t token;   /**< short term authentication of remote client */
+       uuid_stringz_t uuid;    /**< indentification of client seesion */
 };
 
 /**
@@ -79,14 +79,14 @@ static struct {
        int max;                /**< maximum count of sessions */
        int timeout;            /**< common initial timeout */
        struct afb_session *heads[HEADCOUNT]; /**< sessions */
-       uuid_stringz_t initok;  /**< common initial token */
+       struct afb_token *initok;/**< common initial token */
        pthread_mutex_t mutex;  /**< declare a mutex to protect hash table */
 } sessions = {
        .count = 0,
        .max = 10,
        .timeout = 3600,
        .heads = { 0 },
-       .initok = { 0 },
+       .initok = 0,
        .mutex = PTHREAD_MUTEX_INITIALIZER
 };
 
@@ -249,7 +249,6 @@ static struct afb_session *session_add(const char *uuid, int timeout, time_t now
        pthread_mutex_init(&session->mutex, NULL);
        session->refcount = 1;
        strcpy(session->uuid, uuid);
-       strcpy(session->token, sessions.initok);
        session->timeout = timeout;
        session_update_expiration(session, now);
 
@@ -305,10 +304,12 @@ static time_t sessionset_cleanup (int force)
  * @param max_session_count  maximum allowed session count in the same time
  * @param timeout            the initial default timeout of sessions
  * @param initok             the initial default token of sessions
- *
  */
 int afb_session_init (int max_session_count, int timeout, const char *initok)
 {
+       int rc;
+       uuid_stringz_t uuid;
+
        /* check parameters */
        if (initok && strlen(initok) >= sizeof sessions.initok) {
                ERROR("initial token '%s' too long (max length %d)",
@@ -322,10 +323,16 @@ int afb_session_init (int max_session_count, int timeout, const char *initok)
        sessionset_cleanup(1);
        sessions.max = max_session_count;
        sessions.timeout = timeout;
-       if (initok == NULL)
-               uuid_new_stringz(sessions.initok);
-       else
-               strcpy(sessions.initok, initok);
+       if (initok == NULL) {
+               uuid_new_stringz(uuid);
+               initok = uuid;
+       }
+       sessions.initok = 0;
+       if (*initok) {
+               rc = afb_token_get(&sessions.initok, initok);
+               if (rc < 0)
+                       return rc;
+       }
        sessionset_unlock();
        return 0;
 }
@@ -367,7 +374,7 @@ void afb_session_purge()
  */
 const char *afb_session_initial_token()
 {
-       return sessions.initok;
+       return sessions.initok ? afb_token_string(sessions.initok) : "";
 }
 
 /* Searchs the session of 'uuid' */
@@ -517,46 +524,12 @@ int afb_session_is_closed (struct afb_session *session)
        return session->closed;
 }
 
-/*
- * check whether the token of 'session' is 'token'
- * return 1 if true or 0 otherwise
- */
-int afb_session_check_token (struct afb_session *session, const char *token)
-{
-       int r;
-
-       session_lock(session);
-       r = !session->closed
-         && session->expiration >= NOW
-         && !(session->token[0] && strcmp (token, session->token));
-       session_unlock(session);
-       return r;
-}
-
-/* generate a new token and update client context */
-void afb_session_new_token (struct afb_session *session)
-{
-       session_lock(session);
-       uuid_new_stringz(session->token);
-       session_update_expiration(session, NOW);
-#if WITH_AFB_HOOK
-       afb_hook_session_renew(session);
-#endif
-       session_unlock(session);
-}
-
 /* Returns the uuid of 'session' */
 const char *afb_session_uuid (struct afb_session *session)
 {
        return session->uuid;
 }
 
-/* Returns the token of 'session' */
-const char *afb_session_token (struct afb_session *session)
-{
-       return session->token;
-}
-
 /**
  * Get the index of the 'key' in the cookies array.
  * @param key the key to scan
@@ -579,7 +552,7 @@ static int cookeyidx(const void *key)
  * @param makecb       the creation function or NULL
  * @param freecb       the release function or NULL
  * @param closure      an argument for makecb or the value if makecb==NULL
- * @param replace      a boolean enforcing replecement of the previous value
+ * @param replace      a boolean enforcing replacement of the previous value
  *
  * @return the value of the cookie
  *