This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathoauth_controller.dart
129 lines (116 loc) · 4.55 KB
/
oauth_controller.dart
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
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
part of github_issue_mover;
/// Handles the mapping for all OAuth related endpoints.
@Controller
class OAuthController {
/// Relative path to the Exchange code callback URL.
static const String EXCHANGE_CODE_PATH = "/exchange_code";
/// GitHub scopes we need access to.
static const List<String> GITHUB_SCOPES = const <String>["repo"];
/// OAuth flow Object.
OAuth2Flow _oauthFlow;
/// Catches all un-catched exceptions.
@ExceptionHandler()
String errorCatch(ForceRequest req, Model model) {
return "redirect:/logout?error_message=An%20unexpeceted%20error%20happened";
}
/// Redirects the user to the GitHub API authorization page.
@RequestMapping(value: "/oauth_redirect")
HttpResponse authRedirect(ForceRequest req, Model model) {
HttpResponse response = req.request.response;
response.redirect(Uri.parse(
getOauthGitHubHelper(req.request.requestedUri).createAuthorizeUrl()));
return response;
}
/// Serves the main page of the app.
@RequestMapping(value: "/")
String showPage(ForceRequest req, Model model) {
// If an error cookie is being passed along we delete it and we inject the
// error message in the view
String errorFromCookie = CookieManager.getErrorFromCookie(req.request);
if (errorFromCookie != null) {
model.addAttribute("error", errorFromCookie);
CookieManager.removeErrorCookie(req.request);
}
return "index";
}
/// Logs out the user.
@RequestMapping(value: "/logout")
String logout(ForceRequest req, Model model,
@RequestParam() String error_message) {
if (error_message != "") {
CookieManager.addErrorCookie(req.request, error_message);
}
CookieManager.removeAccessTokenCookie(req.request);
return "redirect:/";
}
/// OAuth Callback endpoint.
@RequestMapping(value: EXCHANGE_CODE_PATH)
dynamic oauthCallback(ForceRequest req, Model model, @RequestParam() String
code, @RequestParam() String error) {
if (error != null && error == "") {
CookieManager.addErrorCookie(req.request, error);
CookieManager.removeAccessTokenCookie(req.request);
return "redirect:/";
} else {
getOauthGitHubHelper(req.request.requestedUri).exchange(code)
.then((ExchangeResponse response) {
CookieManager.addAccessTokenCookie(req.request, response.token);
req.async("redirect:/");
}).catchError((error) {
CookieManager.addErrorCookie(req.request, error);
CookieManager.removeAccessTokenCookie(req.request);
req.async("redirect:/");
});
return req.asyncFuture;
}
}
/// Returns true if we are running on a dev environment.
///
/// We base this on the [Uri] the user requested on the server.
static bool isDev(Uri requestedUri) {
// We are in a dev environment if the host is local.
return requestedUri.host == "localhost"
|| requestedUri.host == "127.0.0.1";
}
/// Returns an instance of the OAuth2Flow.
///
/// We base this on the [Uri] the user requested on the server.
OAuth2Flow getOauthGitHubHelper(Uri requestedUri) {
if (_oauthFlow == null) {
OAuthCredentials credentials =
OAuthCredentials.loadOauthCredentials(dev: isDev(requestedUri));
initGitHub();
_oauthFlow = new OAuth2Flow(credentials.clientId,
credentials.clientSecret,
redirectUri: getRedirectUrl(requestedUri),
scopes: GITHUB_SCOPES);
}
return _oauthFlow;
}
/// Returns the absolute URL to the [exchangeCodePath].
///
/// We base this on the [Uri] the user requested on the server.
static String getRedirectUrl(Uri requestedUri) {
// Probable bug in App Engine Managed VMs.
// The scheme returned is HTTP when it should be HTTPS.
if (isDev(requestedUri)) {
return "${requestedUri.origin}$EXCHANGE_CODE_PATH";
}
// When in prod we force HTTPS.
return "${requestedUri.origin}$EXCHANGE_CODE_PATH"
.replaceFirst("http://", "https://");
}
}