Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/liut/staffio-client
Client package of Staffio in Go
https://github.com/liut/staffio-client
Last synced: 9 days ago
JSON representation
Client package of Staffio in Go
- Host: GitHub
- URL: https://github.com/liut/staffio-client
- Owner: liut
- License: mit
- Created: 2019-10-24T16:00:18.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-19T05:10:50.000Z (10 months ago)
- Last Synced: 2024-04-26T00:16:14.835Z (10 months ago)
- Language: Go
- Size: 124 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Staffio client and general OAuth2 client
===settings with environment
---
```plan
OAUTH_CLIENT_ID=
OAUTH_CLIENT_SECRET=
OAUTH_PREFIX=https://staffio.work
OAUTH_URI_AUTHORIZE=/authorize
OAUTH_URI_TOKEN=/token
OAUTH_URI_INFO=/info/me
OAUTH_REDIRECT_URL=/auth/callback
OAUTH_SCOPES='openid'```
Example for staffio SP
---```go
package mainimport (
"fmt"
"net/http"staffio "github.com/liut/staffio-client"
)func main() {
loginPath := "/auth/login"
staffio.SetLoginPath(loginPath)
staffio.SetAdminPath("/admin")http.HandleFunc(loginPath, staffio.LoginHandler)
http.Handle("/auth/callback", staffio.AuthCodeCallback("admin"))authF1 := staffio.Middleware()
authF1 := staffio.Middleware(staffio.WithRefresh()) // auto refresh token time
authF1 := staffio.Middleware(staffio.WithRefresh(), staffio.WithURI(loginPath)) // auto refresh and redirect
http.Handle("/admin", authF1(http.HandlerFunc(handlerAdminWelcome)))
// more handlers
}func handlerAdminWelcome(w http.ResponseWriter, r *http.Request) {
user := staffio.UserFromContext(r.Context())
fmt.Fprintf(w, "welcome %s", user.Name)
}// Middleware for gin
func Middleware(opts ...staffio.OptFunc) gin.HandlerFunc {
option := staffio.NewOption(opts...)
return func(c *gin.Context) {
user, err := staffio.UserFromRequest(c.Request)
if err != nil {
if option.URI != "" {
c.Redirect(http.StatusFound, option.URI)
} else {
c.AbortWithStatus(http.StatusUnauthorized)
}
return
}
if option.Refresh && user.NeedRefresh() {
user.Refresh()
user.Signin(c.Writer)
}
req := c.Request
c.Request = req.WithContext(staffio.ContextWithUser(req.Context(), user))
c.Next()
}
}// UserFromContext for gin
func UserFromContext(c *gin.Context) (user *User, ok bool) {
return staffio.UserFromContext(c.Request.Context())
}// AuthCodeCallback for gin handler which for Check auth with role[s] when auth-code callback
func AuthCodeCallback(roleName ...string) gin.HandlerFunc {
return gin.WrapH(staffio.AuthCodeCallback(roleName...))
}// HandlerShowMe for gin
func HandlerShowMe(c *gin.Context) {
user, ok := staffio.UserFromContext(c.Request.Context())
if !ok {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
c.JSON(http.StatusOK, gin.H{
"me": user,
})
}```