Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rezaoptic/go-utils
some utils for Go projects.
https://github.com/rezaoptic/go-utils
authentication error-handling go go-package hacktoberfest jwt logger multi-language utils
Last synced: about 4 hours ago
JSON representation
some utils for Go projects.
- Host: GitHub
- URL: https://github.com/rezaoptic/go-utils
- Owner: RezaOptic
- License: mit
- Created: 2020-08-27T11:49:49.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-01T16:27:14.000Z (about 4 years ago)
- Last Synced: 2024-06-20T17:30:07.135Z (5 months ago)
- Topics: authentication, error-handling, go, go-package, hacktoberfest, jwt, logger, multi-language, utils
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-utils
This is utils for Go project.
## auth:
a package for generate and validate access token.
access token contains the following structure.
```go
UserID interface{}
Username *string
AppID *string
Email *string
Status *string
Roles []string
```### Usage:
---
### Initialize:
Import it in your code:
```go
import "github.com/RezaOptic/go-utils/auth"
```at the first, we need to initialize the package for configs.
```go
auth.Init(ACCESS_TOKEN_SECRET, EXPIRE_TIME_IN_SECOND)
```### Generate token:
```go
userClaim := auth.UserClaims{UserID: UserID}
Token, err := userClaim.Token()
if err != nil {
panic(err)
}
fmt.Println(Token)
```### Validate token:
#### **ginMiddleware**:
for adding protection to your route add gin Middleware like this:
```go
Foo.GET("/foo/user/:user_id", auth.JwtAuth(BarController, SELF_ACCESS, OPTIONAL, ROLES))
```
and in your `BarController` can get user token information like this:
```go
UserTokenInfotmation := auth.GetClaimFromContext(c)
````SELF_ACCESS`: if you have `:user_id` param in your path for authorization TokenUserID and `user_id` must be the same.
`OPTIONAL`: if you set this flag to true sending `Authorization` in the header is optional and if you don't send the token `auth.GetClaimFromContext(c)` return an empty struct.
`ROLES`: roles is an array of string token must include these roles.