-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
255 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/greenpau/go-authcrunch | ||
|
||
go 1.20 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/crewjam/saml v0.4.14 | ||
|
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,52 @@ | ||
// Copyright 2024 Paul Greenberg [email protected] | ||
// | ||
// 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 role | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Kind is the type of a role. | ||
type Kind int | ||
|
||
const ( | ||
// Unknown operator signals invalid role type. | ||
Unknown Kind = iota | ||
// Anonymous indicates anonymous. | ||
Anonymous | ||
// Admin indicates role with administrative privileges. | ||
Admin | ||
// User indicates role with user privileges. | ||
User | ||
// Guest indicates role with guest privileges. | ||
Guest | ||
) | ||
|
||
// String returns string representation of a role type. | ||
func (e Kind) String() string { | ||
switch e { | ||
case Unknown: | ||
return "Unknown" | ||
case Anonymous: | ||
return "Anonymous" | ||
case Admin: | ||
return "Admin" | ||
case User: | ||
return "User" | ||
case Guest: | ||
return "Guest" | ||
} | ||
return fmt.Sprintf("RoleKind(%d)", int(e)) | ||
} |
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 @@ | ||
// Copyright 2024 Paul Greenberg [email protected] | ||
// | ||
// 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 authn | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
Check failure on line 19 in pkg/authn/gatekeeper.go
|
||
|
||
"github.com/greenpau/go-authcrunch/pkg/authn/enums/role" | ||
"github.com/greenpau/go-authcrunch/pkg/user" | ||
) | ||
|
||
func (p *Portal) authorizedRole(usr *user.User, authorizedRoles []role.Kind, authenticated bool) error { | ||
if !authenticated { | ||
if slices.Contains(authorizedRoles, role.Anonymous) { | ||
return nil | ||
} | ||
return fmt.Errorf("user is not authenticated") | ||
} | ||
|
||
if slices.Contains(authorizedRoles, role.User) { | ||
for roleName := range p.config.PortalUserRoles { | ||
if usr.HasRole(roleName) { | ||
return nil | ||
} | ||
} | ||
for _, roleNamePattern := range p.config.userRolePatterns { | ||
if usr.HasRolePattern(roleNamePattern) { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
if slices.Contains(authorizedRoles, role.Admin) { | ||
for roleName := range p.config.PortalAdminRoles { | ||
if usr.HasRole(roleName) { | ||
return nil | ||
} | ||
} | ||
for _, roleNamePattern := range p.config.adminRolePatterns { | ||
if usr.HasRolePattern(roleNamePattern) { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return fmt.Errorf("user is not authorized") | ||
} |
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
Oops, something went wrong.