diff --git a/main.go b/main.go index f148c2d..8dd629e 100644 --- a/main.go +++ b/main.go @@ -62,6 +62,7 @@ func main() { http.Handle("/oauth/callback", oauthStore.CallbackHandler()) http.Handle("/oauth/login", oauthStore.LoginPage()) + http.Handle("/oauth/unauthorized", oauthStore.UnauthorizedPage()) protectedRoot := false for _, pattern := range config.GuardedPaths { if pattern == "/" { diff --git a/oauth.go b/oauth.go index efd6595..d622102 100644 --- a/oauth.go +++ b/oauth.go @@ -84,6 +84,11 @@ func (s *OAuthStore) DeleteSession(sessionID string) { func sendToLoginPage(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/oauth/login", http.StatusTemporaryRedirect) } + +func sendToUnauthorized(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/oauth/unauthorized", http.StatusTemporaryRedirect) +} + func generateRandomToken() string { b := make([]byte, 32) rand.Read(b) @@ -93,6 +98,16 @@ func generateRandomToken() string { //go:embed templates/LoginPage.html var loginPageContent string +//go:embed templates/NotAuthorizedPage.html +var unauthorizedPageContent string + +func (s *OAuthStore) UnauthorizedPage() http.Handler { + unauthorizedPageTemplate := template.Must(template.New("unauthorizedPageContent").Parse(unauthorizedPageContent)) + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + unauthorizedPageTemplate.Execute(w, nil) + }) +} + func (s *OAuthStore) LoginPage() http.Handler { loginPageTemplate := template.Must(template.New("loginPageContent").Parse(loginPageContent)) @@ -156,7 +171,7 @@ func (s *OAuthStore) Protected(next http.Handler) http.Handler { } } if !found { - sendToLoginPage(w, r) + sendToUnauthorized(w, r) return } diff --git a/templates/NotAuthorizedPage.html b/templates/NotAuthorizedPage.html new file mode 100644 index 0000000..088a6fb --- /dev/null +++ b/templates/NotAuthorizedPage.html @@ -0,0 +1,77 @@ + + +
+ + +