auth via mounted routes

This commit is contained in:
Umputun
2017-12-28 14:40:12 -06:00
parent dfbd9b1829
commit 0b04c16dfb
3 changed files with 18 additions and 8 deletions
+4 -3
View File
@@ -42,9 +42,8 @@ TBD
### Authorization
- `GET /login/{provider}?from=http://url` - perform "social" login with one of supported providers and redirect to `url`
- `GET /logout` - logout
- `GET /api/v1/user` - get user info, _auth required_
- `GET /auth/{provider}/login?from=http://url` - perform "social" login with one of supported providers and redirect to `url`
- `GET /auth/{provider}/logout` - logout
```go
type User struct {
@@ -108,3 +107,5 @@ Sort can be `time` or `score`. Supported sort order with prefix -/+, i.e. `-time
- `PUT /api/v1/admin/user/{userid}?site=site-id&block=1` - block or unblock user. _auth and admin required_
- `GET /api/v1/admin/export?site=side-id&block=1` - export all comments. _auth and admin required_
- `PUT /api/v1/admin/pin/{id}?site=site-id&url=post-url&pin=1` - pin or unpin comment. _auth and admin required_
- `GET /api/v1/user` - get user info, _auth required_
+10
View File
@@ -11,6 +11,7 @@ import (
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/gorilla/sessions"
"golang.org/x/oauth2"
@@ -57,6 +58,15 @@ func initProvider(p Params, provider Provider) *Provider {
return &provider
}
// Routes returns auth routes for given provider
func (p Provider) Routes() chi.Router {
router := chi.NewRouter()
router.Get("/login", p.LoginHandler)
router.Get("/callback", p.AuthHandler)
router.Get("/logout", p.LogoutHandler)
return router
}
// LoginHandler - GET /login/{provider}?from=redirect-back-url
func (p Provider) LoginHandler(w http.ResponseWriter, r *http.Request) {
+4 -5
View File
@@ -62,11 +62,8 @@ func (s *Server) Run() {
// If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler
router.Use(context.ClearHandler)
router.Get("/login/google", s.AuthGoogle.LoginHandler)
router.Get("/auth/google", s.AuthGoogle.AuthHandler)
router.Get("/logout", s.AuthGithub.LogoutHandler) // can hit any provider
router.Get("/login/github", s.AuthGithub.LoginHandler)
router.Get("/auth/github", s.AuthGithub.AuthHandler)
router.Mount("/auth/google", s.AuthGoogle.Routes())
router.Mount("/auth/github", s.AuthGithub.Routes())
router.Route("/api/v1", func(rapi chi.Router) {
rapi.Get("/find", s.findCommentsCtrl)
@@ -75,11 +72,13 @@ func (s *Server) Run() {
rapi.Get("/last/{max}", s.lastCommentsCtrl)
rapi.Get("/count", s.countCtrl)
// require auth
rapi.With(auth.Auth(s.SessionStore, s.Admins, applyDevMode(auth.Full))).Group(func(rauth chi.Router) {
rauth.Post("/comment", s.createCommentCtrl)
rauth.Get("/user", s.userInfoCtrl)
rauth.Put("/vote/{id}", s.voteCtrl)
// require admin
s.mod = admin{dataService: s.DataService, exporter: s.Exporter, respCache: s.respCache}
rauth.Mount("/admin", s.mod.routes())
})