This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Roverr/feature/auth
JWT Authentication
- Loading branch information
Showing
9 changed files
with
264 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package auth | ||
|
||
import ( | ||
"crypto/rsa" | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/Roverr/rtsp-stream/core/config" | ||
jwt "github.com/dgrijalva/jwt-go" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// JWT interface describes how token validation looks like | ||
type JWT interface { | ||
Validate(token string) bool | ||
} | ||
|
||
// JWTProvider implements the validate method | ||
type JWTProvider struct { | ||
secret []byte | ||
verifyKey *rsa.PublicKey | ||
} | ||
|
||
// Implementation check | ||
var _ JWT = (*JWTProvider)(nil) | ||
|
||
// NewJWTProvider returns a new pointer for the created provider | ||
func NewJWTProvider(settings config.Auth) (*JWTProvider, error) { | ||
switch strings.ToLower(settings.JWTMethod) { | ||
case "rsa": | ||
verifyBytes, err := ioutil.ReadFile(settings.JWTPubKeyPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
verifyKey, err := jwt.ParseRSAPublicKeyFromPEM(verifyBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &JWTProvider{verifyKey: verifyKey}, nil | ||
default: | ||
return &JWTProvider{secret: []byte(settings.JWTSecret)}, nil | ||
} | ||
} | ||
|
||
// Validate is for validating if the given token is authenticated | ||
func (jp JWTProvider) Validate(tokenString string) bool { | ||
ts := strings.Replace(tokenString, "Bearer ", "", -1) | ||
token, err := jwt.Parse(ts, jp.verify) | ||
if err != nil { | ||
logrus.Errorln("Error at token verification ", err) | ||
return false | ||
} | ||
return token.Valid | ||
} | ||
|
||
// verify is to check the signing method and return the secret | ||
func (jp JWTProvider) verify(token *jwt.Token) (interface{}, error) { | ||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); ok { | ||
return jp.secret, nil | ||
} | ||
if _, ok := token.Method.(*jwt.SigningMethodRSA); ok { | ||
return jp.verifyKey, nil | ||
} | ||
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) | ||
} |
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,36 @@ | ||
package auth | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/Roverr/rtsp-stream/core/config" | ||
jwt "github.com/dgrijalva/jwt-go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJWTAuthWithSecret(t *testing.T) { | ||
spec := config.InitConfig() | ||
provider, err := NewJWTProvider(spec.Auth) | ||
assert.Nil(t, err) | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{}) | ||
tokenString, err := token.SignedString([]byte(spec.Auth.JWTSecret)) | ||
assert.Nil(t, err) | ||
assert.True(t, provider.Validate(tokenString)) | ||
assert.True(t, provider.Validate(fmt.Sprintf("Bearer %s", tokenString))) | ||
} | ||
|
||
func TestJWTAuthWithRSA(t *testing.T) { | ||
reader := rand.Reader | ||
bitSize := 2048 | ||
key, err := rsa.GenerateKey(reader, bitSize) | ||
assert.Nil(t, err) | ||
publicKey := key.PublicKey | ||
provider := JWTProvider{verifyKey: &publicKey} | ||
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{}) | ||
tokenString, err := token.SignedString(key) | ||
assert.Nil(t, err) | ||
assert.True(t, provider.Validate(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
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
Oops, something went wrong.