-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement authentication for api gateway with api tokens
- Loading branch information
1 parent
f315dac
commit 1b3054f
Showing
15 changed files
with
157 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package HTTP_API | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"time" | ||
) | ||
|
||
func (a *Controller) generateApiToken(c *gin.Context) { | ||
|
||
id, err := primitive.ObjectIDFromHex(c.Param("userId")) | ||
if err != nil { | ||
c.JSON(400, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
expirationTime := time.Now().AddDate(0, 0, 30) | ||
|
||
tokenString, err := a.logic.GenerateApiToken(id, expirationTime) | ||
if err != nil { | ||
c.JSON(500, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(200, gin.H{"api_token": tokenString}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package HTTP_API | ||
|
||
import ( | ||
"errors" | ||
"github.com/dgrijalva/jwt-go" | ||
"github.com/gin-gonic/gin" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
/* | ||
Middleware for checking api key/token for each request. | ||
*/ | ||
func (a *Controller) checkAuth() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
var jwtString string | ||
var err error | ||
|
||
tokenHeader := c.GetHeader("Authorization") | ||
if len(tokenHeader) > 0 { | ||
headerArr := strings.Split(tokenHeader, " ") | ||
if len(headerArr) != 2 { | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
jwtString = headerArr[1] | ||
} else { | ||
jwtString, err = c.Cookie("JWT") | ||
if err != nil || jwtString == "" { | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
} | ||
|
||
token, err := jwt.Parse(jwtString, func(token *jwt.Token) (interface{}, error) { | ||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
return nil, errors.New("api token is not valid") | ||
} | ||
return a.jwtSecret, nil | ||
}) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { | ||
userId, err := primitive.ObjectIDFromHex(claims["id"].(string)) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
c.Set("userId", userId) | ||
} else { | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package Logic | ||
|
||
import ( | ||
"API_GatewayWeb/DataStructures" | ||
"fmt" | ||
"github.com/dgrijalva/jwt-go" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"time" | ||
) | ||
|
||
func (c *Controller) GenerateApiToken(userId primitive.ObjectID, expirationTime time.Time) (tokenString string, err error) { | ||
|
||
// First should also check if the user exists in the database | ||
// and has paid for the service or has the right to access the service, etc. | ||
// We should also save api tokens in key-value store like Redis so that we can | ||
// invalidate the token if needed. | ||
|
||
tk := &DataStructures.ApiKey{ | ||
Id: userId, | ||
StandardClaims: jwt.StandardClaims{ | ||
ExpiresAt: expirationTime.Unix(), | ||
}} | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, tk) | ||
|
||
tokenString, err = token.SignedString(c.jwtSecret) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters