-
Notifications
You must be signed in to change notification settings - Fork 22
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 #383 from tranchitella/men-5676
feat: support for Ed25519 server keys for signing the JWT tokens
- Loading branch information
Showing
25 changed files
with
1,015 additions
and
688 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
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 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 @@ | ||
// Copyright 2023 Northern.tech AS | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package jwt | ||
|
||
import ( | ||
"crypto/ed25519" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// JWTHandlerEd25519 is an Ed25519-specific JWTHandler | ||
type JWTHandlerEd25519 struct { | ||
privKey *ed25519.PrivateKey | ||
} | ||
|
||
func NewJWTHandlerEd25519(privKey *ed25519.PrivateKey) *JWTHandlerEd25519 { | ||
return &JWTHandlerEd25519{ | ||
privKey: privKey, | ||
} | ||
} | ||
|
||
func (j *JWTHandlerEd25519) ToJWT(token *Token) (string, error) { | ||
//generate | ||
jt := jwt.NewWithClaims(jwt.SigningMethodEdDSA, &token.Claims) | ||
|
||
//sign | ||
data, err := jt.SignedString(j.privKey) | ||
return data, err | ||
} | ||
|
||
func (j *JWTHandlerEd25519) FromJWT(tokstr string) (*Token, error) { | ||
jwttoken, err := jwt.ParseWithClaims(tokstr, &Claims{}, | ||
func(token *jwt.Token) (interface{}, error) { | ||
if _, ok := token.Method.(*jwt.SigningMethodEd25519); !ok { | ||
return nil, errors.New("unexpected signing method: " + token.Method.Alg()) | ||
} | ||
return j.privKey.Public(), nil | ||
}, | ||
) | ||
|
||
if err == nil { | ||
token := Token{} | ||
if claims, ok := jwttoken.Claims.(*Claims); ok && jwttoken.Valid { | ||
token.Claims = *claims | ||
return &token, nil | ||
} | ||
} | ||
|
||
return nil, ErrTokenInvalid | ||
} |
Oops, something went wrong.