Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gabrielhora/goth
Simple authorization for http.Handler
https://github.com/gabrielhora/goth
authorization go
Last synced: about 1 month ago
JSON representation
Simple authorization for http.Handler
- Host: GitHub
- URL: https://github.com/gabrielhora/goth
- Owner: gabrielhora
- License: mit
- Created: 2020-09-24T22:55:04.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-09-26T18:23:02.000Z (over 4 years ago)
- Last Synced: 2024-06-20T12:37:14.054Z (6 months ago)
- Topics: authorization, go
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Goth (wip)
Centrally define authorization rules for your HTTP handlers.
Example:
```go
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
_, _ = fmt.Fprint(w, "index")
})mux.HandleFunc("/admin/", func(w http.ResponseWriter, req *http.Request) {
_, _ = fmt.Fprint(w, "admin")
})mux.HandleFunc("/login", func(w http.ResponseWriter, req *http.Request) {
_, _ = fmt.Fprint(w, "login")
})g := goth.New(mux, auth)
g.LoginURL("/login")
// anything inside /admin requires "admin" or "staff" roles
g.Rule("/admin/?(.*)", "admin,staff")
// anything else allow any user
g.Rule("/?(.*)", "annon")server := http.Server{
Addr: ":8080",
Handler: g,
}_ = server.ListenAndServe()
}// this function should return the current user and it's roles, you are
// free to choose how to get this info (from session, cookie, header, etc)
func auth(req *http.Request) (user interface{}, roles []string, err error) {
return 1, []string{"user"}, nil
}
```