-
Notifications
You must be signed in to change notification settings - Fork 60
/
d_api_openid_login.go
73 lines (58 loc) · 1.53 KB
/
d_api_openid_login.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
package uadmin
import (
"context"
"net/http"
"strings"
)
func dAPIOpenIDLoginHandler(w http.ResponseWriter, r *http.Request, s *Session) {
_ = s
redirectURI := r.FormValue("redirect_uri")
Trail(DEBUG, "HERE")
if r.Method == "GET" {
if session := IsAuthenticated(r); session != nil {
Preload(session, "User")
c := map[string]interface{}{
"SiteName": SiteName,
"Language": getLanguage(r),
"RootURL": RootURL,
"Logo": Logo,
"user": s.User,
"OpenIDWebsiteURL": redirectURI,
}
RenderHTML(w, r, "./templates/uadmin/"+Theme+"/openid_concent.html", c)
return
}
http.Redirect(w, r, RootURL+"login/?next="+RootURL+"api/d/auth/openidlogin?"+r.URL.Query().Encode(), 303)
return
}
if s == nil {
w.WriteHeader(http.StatusUnauthorized)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Invalid credentials",
})
return
}
// Preload the user to get the group name
Preload(&s.User)
ctx := context.WithValue(r.Context(), CKey("aud"), getAUD(redirectURI))
r = r.WithContext(ctx)
jwt := createJWT(r, s)
http.Redirect(w, r, redirectURI+"?access-token="+jwt, 303)
}
func getAUD(URL string) string {
aud := ""
if strings.HasPrefix(URL, "https://") {
aud = "https://"
URL = strings.TrimPrefix(URL, "https://")
}
if strings.HasPrefix(URL, "http://") {
aud = "http://"
URL = strings.TrimPrefix(URL, "http://")
}
if strings.Contains(URL, "/") {
URL = URL[:strings.Index(URL, "/")]
aud += URL
}
return aud
}