-
Notifications
You must be signed in to change notification settings - Fork 0
/
authhandlers.go
135 lines (125 loc) · 3.64 KB
/
authhandlers.go
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
package googleauth
import (
"html/template"
"net/http"
"github.com/golang/glog"
)
var (
// Name of the top-level login template.
TemplateName = "login"
// Template to use for the login redirect.
LoginTmpl = template.Must(template.New(TemplateName).Parse(tmpl))
tmpl = `{{define "scripts"}}
<script src="https://apis.google.com/js/client:platform.js" async defer></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var helper = (function() {
var BASE_API_PATH = 'plus/v1/';
var authResult = undefined;
return {
/**
* Hides the sign-in button and connects the server-side app after
* the user successfully signs in.
*/
signInCallback: function(authResult) {
if (authResult['access_token']) {
this.logIn(authResult.code);
} else if (authResult["error"]) {
$('#authResult').append('Logged out');
}
},
logIn: function(code) {
window.location.replace(
window.location.origin + "/connect?state={{.StateToken}}&code=" + code);
}
};
})();
function signInCallback(authResult) {
helper.signInCallback(authResult);
}
</script>
{{end}}
{{define "body"}}
<div id="gConnect">
<span id="signinButton">
<span
class="g-signin"
data-callback="signInCallback"
data-clientid="{{.ClientId}}"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-scope="profile">
</span>
</span>
</div>
{{template "scripts" .}}
{{end}}
{{define "login"}}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
{{template "body" .}}
</body>
</html>
{{end}}`
)
// RequireLogin returns a wrapped HandlerFunc that enforces Google+ login.
//
// If the user is logged in, the specified HandlerFunc is called, otherwise the
// login page defined by LoginTmpl is served.
//
// RequireLogin returns HTTP 500 (Internal Server Error) if the
// template fails to render or the package has an internal error.
func RequireLogin(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
loggedIn, err := IsLoggedIn(r)
if err != nil {
glog.Errorf("failed to get login info: %v\n", err)
http.Error(w, "Internal server error.", http.StatusInternalServerError)
return
}
if loggedIn {
glog.V(1).Infof("user is logged in, onward to original handler func\n")
fn(w, r)
return
}
glog.V(1).Infof("not logged in, fetching state token\n")
li, err := LogIn(w, r)
if err != nil {
glog.Errorf("failed to get login info: %v\n", err)
http.Error(w, "Internal server error.", http.StatusInternalServerError)
return
}
err = LoginTmpl.ExecuteTemplate(w, TemplateName, li)
if err != nil {
glog.Errorf("failed to execute login template: %v\n", err)
http.Error(w, "Internal server error.", http.StatusInternalServerError)
return
}
}
}
// ConnectHandler finishes the connection process, exchanging the
// one-time authorization code for an access token and storing it in
// the session.
//
// ConnectHandler redirects to the request "referer" on successful login.
//
// ConnectHandler returns HTTP 401 (Unauthorized) if the user does not
// have access.
func ConnectHandler(w http.ResponseWriter, r *http.Request) {
err := Connect(w, r)
if err != nil {
if isAccessDenied(err) {
http.Error(w, "Access denied.", http.StatusUnauthorized)
} else {
glog.Errorf("error connecting to googleauth: %v\n", err)
http.Error(w, "Internal server error.", http.StatusInternalServerError)
}
return
}
glog.V(1).Infof("current user is connected, redirecting to %q\n", r.Referer())
http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
}