Skip to content

Commit

Permalink
move splituserid
Browse files Browse the repository at this point in the history
  • Loading branch information
havardelnan committed Apr 15, 2024
1 parent f66da59 commit fe5f6ce
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 31 deletions.
14 changes: 14 additions & 0 deletions pkg/auth/authtools/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package authtools

import (
"fmt"
"strings"
)

func SplitUserId(userId string) (string, string, error) {
parts := strings.Split(userId, "@")
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid userId: %s", userId)
}
return parts[0], parts[1], nil
}
12 changes: 2 additions & 10 deletions pkg/auth/userauth/activedirectory/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"net"
"regexp"
"strconv"
"strings"
"time"

"github.com/NorskHelsenett/ror/pkg/auth/authtools"
identitymodels "github.com/NorskHelsenett/ror/pkg/models/identity"
"github.com/NorskHelsenett/ror/pkg/rlog"
newhealth "github.com/dotse/go-health"
Expand Down Expand Up @@ -149,7 +149,7 @@ func (l *AdClient) search(basedn, filter string, attributes []string) (*ldap.Sea

func (l *AdClient) GetUser(userId string) (*identitymodels.User, error) {

userpart, domainpart, err := splitUserId(userId)
userpart, domainpart, err := authtools.SplitUserId(userId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -201,14 +201,6 @@ func (l *AdClient) GetUser(userId string) (*identitymodels.User, error) {
return &user, nil
}

func splitUserId(userId string) (string, string, error) {
parts := strings.Split(userId, "@")
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid userId: %s", userId)
}
return parts[0], parts[1], nil
}

func checkUserAccountControl(userAccountControl string) error {
intuac, err := strconv.ParseUint(userAccountControl, 10, 64)
if err != nil {
Expand Down
12 changes: 2 additions & 10 deletions pkg/auth/userauth/ldaps/openldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/NorskHelsenett/ror/pkg/auth/authtools"
identitymodels "github.com/NorskHelsenett/ror/pkg/models/identity"
"github.com/NorskHelsenett/ror/pkg/rlog"
newhealth "github.com/dotse/go-health"
Expand Down Expand Up @@ -110,7 +110,7 @@ func (l *LdapsClient) search(basedn, filter string, attributes []string) (*ldap.

func (l *LdapsClient) GetUser(userId string) (*identitymodels.User, error) {

_, domainpart, err := splitUserId(userId)
_, domainpart, err := authtools.SplitUserId(userId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -157,14 +157,6 @@ func (l *LdapsClient) GetUser(userId string) (*identitymodels.User, error) {
return &user, nil
}

func splitUserId(userId string) (string, string, error) {
parts := strings.Split(userId, "@")
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid userId: %s", userId)
}
return parts[0], parts[1], nil
}

// TODO: Implement
func (l *LdapsClient) CheckHealth() []newhealth.Check {
return []newhealth.Check{
Expand Down
3 changes: 2 additions & 1 deletion pkg/auth/userauth/msgraph/msgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/NorskHelsenett/ror/pkg/auth/authtools"
"github.com/NorskHelsenett/ror/pkg/helpers/kvcachehelper"
"github.com/NorskHelsenett/ror/pkg/helpers/kvcachehelper/memorycache"
identitymodels "github.com/NorskHelsenett/ror/pkg/models/identity"
Expand Down Expand Up @@ -105,7 +106,7 @@ func (g *MsGraphClient) GetUser(userId string) (*identitymodels.User, error) {

func addDomainpartToGroups(groupnames *[]string, userId string) {

_, domain, err := splitUserId(userId)
_, domain, err := authtools.SplitUserId(userId)
if err != nil {
domain = ""
}
Expand Down
12 changes: 2 additions & 10 deletions pkg/auth/userauth/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/NorskHelsenett/ror/pkg/auth/authtools"
"github.com/NorskHelsenett/ror/pkg/auth/userauth/activedirectory"
"github.com/NorskHelsenett/ror/pkg/auth/userauth/ldaps"
"github.com/NorskHelsenett/ror/pkg/auth/userauth/msgraph"
Expand Down Expand Up @@ -35,7 +35,7 @@ type DomainResolvers struct {
}

func (d DomainResolvers) GetUser(userId string) (*identitymodels.User, error) {
domain, _, err := splitUserId(userId)
_, domain, err := authtools.SplitUserId(userId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -69,14 +69,6 @@ func (d DomainResolvers) RegisterHealthChecks() {
}
}

func splitUserId(userId string) (string, string, error) {
parts := strings.Split(userId, "@")
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid userId: %s", userId)
}
return parts[1], parts[0], nil
}

func NewDomainResolversFromJson(jsonBytes []byte) (*DomainResolvers, error) {
var domainResolverConfigs DomainResolverConfigs
var domainResolvers *DomainResolvers = &DomainResolvers{}
Expand Down

0 comments on commit fe5f6ce

Please sign in to comment.