-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_sig_check.vcl
134 lines (115 loc) · 3.04 KB
/
oauth_sig_check.vcl
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
table consumer_secrets {
"foo": "foo_secret"
}
table access_tokens {
"bar": "bar_secret"
}
sub vcl_recv {
#FASTLY recv
#7D_DEPLOY recv
declare local var.consumer_key STRING;
declare local var.consumer_secret STRING;
declare local var.access_token STRING;
declare local var.access_token_secret STRING;
declare local var.provided_signature STRING;
declare local var.parameters STRING;
declare local var.base_string_uri STRING;
declare local var.base_string STRING;
declare local var.calculated_signature STRING;
declare local var.timestamp_parameter STRING;
declare local var.timestamp TIME;
declare local var.max_timestamp_age TIME;
declare local var.max_timestamp_in_future TIME;
set var.max_timestamp_age = 30m;
set var.max_timestamp_in_future = 1m;
set req.url = boltsort.sort(req.url);
set var.consumer_key = if(req.url ~ "(?i)oauth_consumer_key=([^&]*)",
urldecode(re.group.1), "");
if(var.consumer_key == "") {
error 401 "Missing Consumer Key";
}
set var.consumer_secret = table.lookup(consumer_secrets, var.consumer_key, "");
if(var.consumer_secret == "") {
error 401 "Invalid Consumer Key";
}
set var.access_token = if(req.url ~ "(?i)oauth_token=([^&]*)",
urldecode(re.group.1), "");
if(var.access_token != "") {
set var.access_token_secret = table.lookup(access_tokens, var.access_token, "");
if(var.access_token_secret == "") {
error 401 "Invalid Access Token";
}
} else {
set var.access_token_secret = "";
}
set var.provided_signature = if(req.url ~ "(?i)oauth_signature=([^&]*)",
urldecode(re.group.1), "");
if(var.provided_signature == "") {
error 401 "Missing Signature";
}
set var.parameters = regsub(
req.url.qs,
"&oauth_signature=[^&]*", "");
set var.base_string_uri =
if(req.http.Fastly-SSL, "https", "http")
"://"
std.tolower(req.http.host)
req.url.path;
set var.base_string =
req.request
"&"
urlencode(var.base_string_uri)
"&"
urlencode(var.parameters);
set var.calculated_signature = digest.hmac_sha1_base64(
var.consumer_secret "&" var.access_token_secret,
var.base_string);
if(!digest.secure_is_equal(var.provided_signature,
var.calculated_signature)) {
error 401 "Invalid OAuth Signature";
}
set var.timestamp_parameter = if(req.url ~ "(?i)oauth_timestamp=([0-9]*)",
urldecode(re.group.1), "");
if(var.timestamp_parameter == "") {
error 401 "Missing/Invalid Timestamp";
}
set var.timestamp = std.integer2time(std.atoi(var.timestamp_parameter));
if(time.is_after(
now,
time.add(var.timestamp, var.max_timestamp_age))) {
error 401 "Timestamp expired";
}
if(time.is_after(
var.timestamp,
time.add(now, var.max_timestamp_in_future))) {
error 401 "Timestamp too far in future";
}
error 200 "Authenticated!";
return(lookup);
}
sub vcl_fetch {
#FASTLY fetch
return(deliver);
}
sub vcl_hit {
#FASTLY hit
if (!obj.cacheable) {
return(pass);
}
return(deliver);
}
sub vcl_miss {
#FASTLY miss
return(fetch);
}
sub vcl_deliver{
#FASTLY deliver
return(deliver);
}
sub vcl_error {
#FASTLY error
#7D_DEPLOY error
}
sub vcl_pass {
#FASTLY pass
}