aboutsummaryrefslogtreecommitdiff
path: root/server/src/jchat_utils.erl
blob: 3f94cfb194cb62dd3cc826cf62f8b696f10a99c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
-module(jchat_utils).

-export([now_iso8601/0, 
         generate_id/0,
         validate_id/1,
         json_encode/1,
         json_decode/1,
         format_error/1,
         validate_account_id/1,
         extract_auth_token/1,
         validate_method_call/1,
         format_method_response/3,
         format_error_response/3]).

%% Generate ISO8601 timestamp
now_iso8601() ->
    {{Year, Month, Day}, {Hour, Minute, Second}} = calendar:universal_time(),
    iolist_to_binary(io_lib:format("~4..0w-~2..0w-~2..0wT~2..0w:~2..0w:~2..0wZ",
                                   [Year, Month, Day, Hour, Minute, Second])).

%% Generate a new ID (UUID v4)
generate_id() ->
    <<U0:32, U1:16, _:4, U2:12, _:2, U3:62>> = crypto:strong_rand_bytes(16),
    iolist_to_binary(io_lib:format("~8.16.0b-~4.16.0b-4~3.16.0b-~4.16.0b-~12.16.0b", 
                                   [U0, U1, U2, ((U3 bsr 60) band 16#03) bor 16#08, U3 band 16#0fffffffffff])).

%% Validate ID format (URL-safe base64 characters)
validate_id(Id) when is_binary(Id) ->
    Size = byte_size(Id),
    case Size >= 1 andalso Size =< 255 of
        true ->
            validate_id_chars(Id);
        false ->
            false
    end;
validate_id(_) ->
    false.

validate_id_chars(<<>>) ->
    true;
validate_id_chars(<<C, Rest/binary>>) ->
    case is_valid_id_char(C) of
        true -> validate_id_chars(Rest);
        false -> false
    end.

is_valid_id_char(C) ->
    (C >= $A andalso C =< $Z) orelse
    (C >= $a andalso C =< $z) orelse
    (C >= $0 andalso C =< $9) orelse
    C =:= $- orelse C =:= $_.

%% JSON encoding/decoding
json_encode(Term) ->
    jsx:encode(Term).

json_decode(JSON) ->
    try
        {ok, jsx:decode(JSON, [return_maps])}
    catch
        _:_ -> {error, invalid_json}
    end.

%% Format errors for JMAP responses (RFC 8620 compliant)
format_error(not_found) ->
    #{type => <<"notFound">>};
format_error(invalid_arguments) ->
    #{type => <<"invalidArguments">>};
format_error(account_not_found) ->
    #{type => <<"accountNotFound">>};
format_error(forbidden) ->
    #{type => <<"forbidden">>};
format_error(invalid_result_reference) ->
    #{type => <<"invalidResultReference">>};
format_error(anchor_not_found) ->
    #{type => <<"anchorNotFound">>};
format_error(unsupported_sort) ->
    #{type => <<"unsupportedSort">>};
format_error(unsupported_filter) ->
    #{type => <<"unsupportedFilter">>};
format_error(cannot_calculate_changes) ->
    #{type => <<"cannotCalculateChanges">>};
format_error(too_large) ->
    #{type => <<"tooLarge">>};
format_error(too_many_changes) ->
    #{type => <<"tooManyChanges">>};
format_error(rate_limited) ->
    #{type => <<"rateLimited">>};
format_error(request_too_large) ->
    #{type => <<"requestTooLarge">>};
format_error(limit_exceeded) ->
    #{type => <<"limitExceeded">>};
format_error(state_mismatch) ->
    #{type => <<"stateMismatch">>};
format_error(will_destroy) ->
    #{type => <<"willDestroy">>};
format_error({invalid_arguments, Description}) ->
    #{type => <<"invalidArguments">>, description => Description};
format_error({not_found, Description}) ->
    #{type => <<"notFound">>, description => Description};
format_error({forbidden, Description}) ->
    #{type => <<"forbidden">>, description => Description};
format_error({server_fail, Description}) ->
    #{type => <<"serverFail">>, description => Description};
format_error(Error) ->
    #{type => <<"serverFail">>, description => iolist_to_binary(io_lib:format("~p", [Error]))}.

%% JMAP-specific utility functions

%% Validate account ID format (as per JMAP spec)
validate_account_id(AccountId) when is_binary(AccountId) ->
    validate_id(AccountId);
validate_account_id(_) ->
    false.

%% Extract Bearer token from Authorization header
extract_auth_token(undefined) ->
    {error, no_auth_header};
extract_auth_token(<<"Bearer ", Token/binary>>) ->
    {ok, Token};
extract_auth_token(_) ->
    {error, invalid_auth_format}.

%% Validate JMAP method call structure
validate_method_call([Method, Args, CallId]) 
  when is_binary(Method), is_map(Args), is_binary(CallId) ->
    case validate_method_name(Method) of
        true -> ok;
        false -> {error, invalid_method_name}
    end;
validate_method_call(_) ->
    {error, invalid_method_call_structure}.

validate_method_name(Method) ->
    case binary:split(Method, <<"/">>) of
        [Type, Operation] when byte_size(Type) > 0, byte_size(Operation) > 0 ->
            validate_method_chars(Method);
        _ ->
            false
    end.

validate_method_chars(<<>>) ->
    true;
validate_method_chars(<<C, Rest/binary>>) ->
    case is_valid_method_char(C) of
        true -> validate_method_chars(Rest);
        false -> false
    end.

is_valid_method_char(C) ->
    (C >= $A andalso C =< $Z) orelse
    (C >= $a andalso C =< $z) orelse
    (C >= $0 andalso C =< $9) orelse
    C =:= $/ orelse C =:= $_.

%% Format successful method response
format_method_response(Method, Result, CallId) ->
    [Method, Result, CallId].

%% Format error method response
format_error_response(Error, CallId, Method) ->
    ErrorMap = format_error(Error),
    [<<"error">>, ErrorMap#{<<"description">> => <<"Error in method: ", Method/binary>>}, CallId].