forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
steam.go
53 lines (45 loc) · 1.28 KB
/
steam.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
package servermanager
import (
"fmt"
"io"
"net/http"
"github.com/sirupsen/logrus"
"github.com/solovev/steam_go"
)
type SteamLoginHandler struct{}
func (slh *SteamLoginHandler) redirectToSteamLogin(backURLFunc func(r *http.Request) string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
opID := steam_go.NewOpenId(r)
switch opID.Mode() {
case "":
http.Redirect(w, r, opID.AuthUrl(), http.StatusPermanentRedirect)
case "cancel":
logrus.Error("Steam authorization cancelled")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
default:
steamID, err := opID.ValidateAndGetId()
if err != nil {
logrus.WithError(err).Error("Could not validate steamID")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
clientSideRedirect(backURLFunc(r)+"?steamGUID="+steamID, w)
}
}
}
const clientSideRedirectHTML = `
<!doctype html>
<html>
<head>
<title>Redirect</title>
<meta http-equiv="refresh" content="0;URL='%s'" />
</head>
<body>
<p>If you are not redirected automatically, please <a href="%s">click here</a>.</p>
</body>
</html>
`
func clientSideRedirect(url string, w io.Writer) {
_, _ = w.Write([]byte(fmt.Sprintf(clientSideRedirectHTML, url, url)))
}