Skip to content

Commit 12ff90f

Browse files
committed
New version added
1 parent ebe33cd commit 12ff90f

File tree

18 files changed

+440
-246
lines changed

18 files changed

+440
-246
lines changed

README.md

+23-88
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# Golang Oauth 2.0 with JWT custom server with example
22
[![Build][Build-Status-Image]][Build-Status-Url] [![Go Report Card](https://goreportcard.com/badge/github.com/gobeam/goOauth2?branch=master)](https://goreportcard.com/report/github.com/gobeam/goOauth2) [![GoDoc][godoc-image]][godoc-url]
33

4-
If you're searching for making custom Oauth 2.0 server your search has finished here. This package helps you to develop your own custom oauth2 server. With lots of scaffolding done for you you can easily implement your own logic without any hassle.
4+
Build your own Golang custom Oauth 2.0 server. This package helps you to develop your own custom oauth2 server. With lots of scaffolding done for you you can easily implement your own logic without any hassle.
55
<br>
66
Official docs: [Here](https://godoc.org/github.com/gobeam/goOauth2)
77

88
* [Why?](#why)
99
* [Example](#example)
1010
* [Installation](#installation)
1111
* [Initialization](#initialization)
12-
* [Functions](#functions)
12+
* [Create Client](#create-client)
13+
* [Create Access Token](create-access-token)
14+
* [Revoke Access/Refresh Token manually](#revoke-accessrefresh-token-manually)
15+
* [Clear All Access Token Of User](#clear-all-access-token-of-user)
1316
* [Running the tests](#running-the-tests)
1417
* [Contributing](#contributing)
1518
* [License](#license)
@@ -23,7 +26,7 @@ This package uses <b>EncryptOAEP</b> which encrypts the given data with <b>RSA-O
2326

2427

2528
## Example
26-
For easy scaffold full working REST API example made with framework [gin-gonic/gin](https://github.com/gin-gonic/gin) is included in [example](https://github.com/gobeam/golang-oauth/tree/master/example) implementing this package.
29+
For easy scaffold and full working REST API example made with framework [gin-gonic/gin](https://github.com/gin-gonic/gin) is included in [example](https://github.com/gobeam/golang-oauth/tree/master/example) implementing this package.
2730

2831

2932
## Installation
@@ -68,98 +71,30 @@ To create client where 1 is user ID Which will return Oauth Clients struct which
6871
```
6972

7073

71-
72-
## Usage
73-
74-
``` go
75-
package main
76-
77-
import (
78-
_ "github.com/go-sql-driver/mysql"
79-
"github.com/google/uuid"
80-
"github.com/roshanr83/go-oauth2"
81-
"gopkg.in/go-oauth2/mysql.v3"
82-
"time"
83-
)
84-
85-
func main() {
86-
//register store
87-
store := oauth.NewDefaultStore(
88-
oauth.NewConfig("root:root@tcp(127.0.0.1:8889)/goauth?charset=utf8&parseTime=True&loc=Local"),
89-
)
90-
defer store.Close()
91-
92-
93-
94-
/* to create client
95-
where 1 is user ID Which will return Oauth Clients
96-
struct which include client id and secret whic is
97-
later used to validate client credentials */
98-
store.CreateClient(userId int64)
74+
## Create Access Token
75+
Visit [oauthMiddleware.go](https://github.com/gobeam/golang-oauth/blob/master/example/middlewares/oauthMiddleware.go) to get full example on how to handle creating access token and refresh token.
9976

10077

78+
## Revoke Access/Refresh Token manually
10179

102-
/* create access token alongside refresh token
103-
Since it will not include user authentication
104-
because it can be different for everyone you will
105-
have to authenticate user and pass user id to Token struct.
106-
Here you will authenticate user and get userID
107-
you will have to provide all the field given below.
108-
ClientID must be valid uuid. AccessExpiresIn is required
109-
to mark expiration time. In response you will get TokenResponse
110-
including accesstoken and refeshtoken. */
111-
accessToken := &oauth.Token{
112-
ClientID: uuid.MustParse("17d5a915-c403-487e-b41f-92fd1074bd30"),
113-
ClientSecret: "UnCMSiJqxFg1O7cqL0MM",
114-
UserID: userID,
115-
Scope: "*",
116-
AccessCreateAt: time.Now(),
117-
AccessExpiresIn: time.Second * 15,
118-
RefreshCreateAt: time.Now(),
119-
}
120-
resp, err := store.Create(accessToken TokenInfo)
121-
122-
123-
124-
/*To check valid accessToken, you should
125-
pass accessToken and it will check if it is valid accesstoken
126-
including if it is valid and non revoked. If it is valid
127-
in response it will return AccessTokens data correspond to that token */
128-
resp, err := store.GetByAccess(accessToken string)
129-
130-
131-
132-
/* To check valid refreshToken, you should pass
133-
refreshToken and it will check if it is valid
134-
refreshToken including if it is valid and non revoked
135-
and if it;s related accessToken is already revoked or
136-
not. If it is valid in response it will return AccessTokens
137-
data correspond to that token*/
138-
/* Note that refresh token after using one time
139-
will be revoked and cannot be used again */
140-
resp, err := store.GetByRefresh(refreshToken string)
141-
142-
143-
144-
/*You can manually revoke access token by passing
145-
userId which you can get from valid token info */
146-
store.RevokeByAccessTokens(userId int64)
147-
148-
149-
150-
/*You can manually revoke refresh token by passing
151-
accessTokenId which you can get from valid token info */
152-
store.RevokeRefreshToken(accessTokenId string)
153-
80+
```go
81+
/*You can manually revoke access token by passing
82+
userId which you can get from valid token info */
83+
store.RevokeByAccessTokens(userId)
84+
85+
/*You can manually revoke refresh token by passing
86+
accessTokenId which you can get from valid token info */
87+
store.RevokeRefreshToken(accessTokenId)
15488

89+
```
15590

156-
/* you can also clear all token related to
157-
user by passing TokenInfo from valid token */
158-
store.ClearByAccessToken(userId int64)
159-
160-
}
16191

92+
## Clear All Access Token Of User
16293

94+
```go
95+
/* you can also clear all token related to
96+
user by passing TokenInfo from valid token */
97+
store.ClearByAccessToken(userId)
16398
```
16499

165100

example/controllers/authController.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (controller AuthController) Register(c *gin.Context) {
3131
}
3232

3333
func (controller AuthController) Client(c *gin.Context) {
34-
client, err := controller.store.CreateClient(1)
34+
client, err := controller.store.CreateClient(1,"test app")
3535
if err != nil {
3636
controller.ErrorResponse(c, http.StatusUnauthorized, err.Error())
3737
return

example/controllers/categoryController.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,32 @@ type CategoryController struct {
1313
Controller
1414
}
1515

16+
17+
func NewCategoryController() *CategoryController {
18+
return &CategoryController{}
19+
}
20+
21+
// Index return all categories
1622
func (controller CategoryController) Index(c *gin.Context) {
1723
var categories models.Categories
1824
categories.Get()
1925
controller.SuccessResponse(c, categories)
2026
}
2127

28+
// View returns category by given id
2229
func (controller CategoryController) View(c *gin.Context) {
2330
var category models.Category
2431
id := c.Param("id")
2532
todoId, _ := strconv.ParseInt(id, 10, 64)
2633
category.FindById(uint(todoId))
2734
if category.ID != 0 {
2835
controller.SuccessResponse(c, category)
36+
return
2937
}
3038
controller.ErrorResponse(c, http.StatusNotFound, "not found")
3139
}
3240

41+
// Store stores new category
3342
func (controller CategoryController) Store(c *gin.Context) {
3443
var category models.Category
3544
if err := c.ShouldBindBodyWith(&category, binding.JSON); err != nil {
@@ -41,6 +50,7 @@ func (controller CategoryController) Store(c *gin.Context) {
4150
controller.SuccessResponse(c, category)
4251
}
4352

53+
// Update updates category by id
4454
func (controller CategoryController) Update(c *gin.Context) {
4555
var category models.Category
4656
if err := c.ShouldBindBodyWith(&category, binding.JSON); err != nil {
@@ -61,17 +71,17 @@ func (controller CategoryController) Update(c *gin.Context) {
6171
controller.SuccessResponse(c, orginalCategory)
6272
}
6373

74+
// Destroy deletes category by id
6475
func (controller CategoryController) Destroy(c *gin.Context) {
6576
var category models.Category
6677
id := c.Param("id")
6778
todoId, _ := strconv.ParseInt(id, 10, 64)
6879
category.FindById(uint(todoId))
6980
if category.ID != 0 {
7081
category.Delete()
82+
controller.Deleted(c)
83+
return
7184
}
72-
controller.Deleted(c)
85+
controller.ErrorResponse(c, http.StatusNotFound, "not found")
7386
}
7487

75-
func NewCategoryController() *CategoryController {
76-
return &CategoryController{}
77-
}

0 commit comments

Comments
 (0)