-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement admin authenticator #5
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2bb048c
feat: implement admin authenticator
1995parham 6fed7b3
fix: correct default configuration
1995parham 4f0c72d
feat: handle superuser field in the API response
1995parham 244f0d6
fix: correct tests and their usage
1995parham 61bca80
fix: correct tests and their usage
1995parham 2e796e3
fix: we have working tests :dancer:
1995parham 325b788
chore: remove debug statement
1995parham 79c67e2
feat: improve test coverage
1995parham 0a6fdce
feat: update testing structure
1995parham ed4d39f
feat: add more tests for auto authenticator
1995parham 0e45e83
fix: correct lint issues
1995parham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package authenticator | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/snapp-incubator/soteria/internal/config" | ||
"github.com/snapp-incubator/soteria/pkg/acl" | ||
) | ||
|
||
// AdminAuthenticator is responsible for Acl/Auth/Token of the internal system users, | ||
// these users have admin access. | ||
type AdminAuthenticator struct { | ||
Key any | ||
Company string | ||
JwtConfig config.Jwt | ||
Parser *jwt.Parser | ||
} | ||
|
||
// Auth check user authentication by checking the user's token | ||
// isSuperuser is a flag that authenticator set it true when credentials is related to a superuser. | ||
func (a AdminAuthenticator) Auth(tokenString string) error { | ||
_, err := a.Parser.Parse(tokenString, func( | ||
token *jwt.Token, | ||
) (interface{}, error) { | ||
claims, ok := token.Claims.(jwt.MapClaims) | ||
if !ok { | ||
return nil, ErrInvalidClaims | ||
} | ||
if claims[a.JwtConfig.IssName] == nil { | ||
return nil, ErrIssNotFound | ||
} | ||
|
||
return a.Key, nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("token is invalid: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ACL check a system user access to a topic. | ||
// because we returns is-admin: true, this endpoint shouldn't | ||
// be called. | ||
func (a AdminAuthenticator) ACL( | ||
_ acl.AccessType, | ||
_ string, | ||
_ string, | ||
) (bool, error) { | ||
return true, nil | ||
} | ||
|
||
func (a AdminAuthenticator) ValidateAccessType(_ acl.AccessType) bool { | ||
return true | ||
} | ||
|
||
func (a AdminAuthenticator) GetCompany() string { | ||
return a.Company | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@1995parham In long term view we can think about restricting services on their topics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right Ahmad jan, EMQ has a concept which creates tenants for you by prefixing topics. We can use this feature for supporting different vendors but right now we need shared topics. For example Snapp Box want to read the Snapp events this means we need to validate Snapp Box tokens for Snapp topics.