Skip to content

Instantly share code, notes, and snippets.

@vishnevskiy
Created May 6, 2010 09:00
Show Gist options
  • Select an option

  • Save vishnevskiy/391941 to your computer and use it in GitHub Desktop.

Select an option

Save vishnevskiy/391941 to your computer and use it in GitHub Desktop.

Revisions

  1. vishnevskiy created this gist May 6, 2010.
    51 changes: 51 additions & 0 deletions gistfile1.hrl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    %% if not value: return None
    %% parts = value.split("|")
    %% if len(parts) != 3: return None
    %% if include_name:
    %% signature = self._cookie_signature(name, parts[0], parts[1])
    %% else:
    %% signature = self._cookie_signature(parts[0], parts[1])
    %% if not _time_independent_equals(parts[2], signature):
    %% logging.warning("Invalid cookie signature %r", value)
    %% return None
    %% timestamp = int(parts[1])
    %% if timestamp < time.time() - 31 * 86400:
    %% logging.warning("Expired cookie %r", value)
    %% return None
    %% try:
    %% return base64.b64decode(parts[0])
    %% except:
    %% return None

    case [Cookie || " session_key=" ++ Cookie <- string:tokens(Req:get_header_value("Cookie"), ";")] of
    [Cookie] ->
    case string:tokens(Cookie, "|") of
    [P0, P1, P2] ->
    Msg = string:concat("session_key", string:concat(P0, P1)),
    Signature = hmac_sha1_hex("11oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", Msg),

    case time_independent_equals(P2, Signature) of
    false ->
    undefined;
    true ->
    {Mega, Secs, _} = erlang:now(),
    {Timestamp, _} = string:to_integer(P1),

    case Timestamp < Mega * 1000000 + Secs - (31 * 86400) of
    false ->
    try base64:decode(P0) of
    Value -> Value
    catch
    _ -> undefined
    end;
    true ->
    undefined
    end
    end;
    _ ->
    undefined
    end;
    [] ->
    undefined
    end.