diff --git a/internal/pkg/services/auth/models.go b/internal/pkg/services/auth/models.go new file mode 100644 index 0000000..6e71b0b --- /dev/null +++ b/internal/pkg/services/auth/models.go @@ -0,0 +1,70 @@ +package auth + +import "github.com/cthit/goldapps/internal/pkg/model" + +type AuthSuperGroup struct { + Name string `json:"name"` + PrettyName string `json:"prettyName"` + Type string `json:"type"` + Members []struct { + Post struct { + PostID string `json:"postId"` + SvText string `json:"svText"` + EnText string `json:"enText"` + EmailPrefix string `json:"emailPrefix"` + } `json:"post"` + User AuthUser `json:"user"` + } `json:"members"` +} + +type AuthUser struct { + Email string `json:"email"` + Cid string `json:"cid"` + FirstName string `json:"firstName"` + LastName string `json:"lastName"` + Nick string `json:"nick"` +} + +type AuthSuperGroups []AuthSuperGroup + +type AuthUsers []AuthUser + +func (user AuthUser) ToUser() model.Users { + return model.Users{ + model.User{ + Cid: user.Cid, + FirstName: user.FirstName, + SecondName: user.LastName, + Nick: user.Nick, + Mail: user.Email, + }, + } +} + +func (users AuthUsers) ToUsers() model.Users { + usersList := model.Users{} + for _, user := range users { + usersList = append(usersList, user.ToUser()...) + } + return usersList +} + +func (superGroup AuthSuperGroup) ToGroup() model.Group { + group := model.Group{ + Email: superGroup.Name + "@chalmers.it", + Type: superGroup.Type, + Aliases: []string{}, + } + for _, member := range superGroup.Members { + group.Members = append(group.Members, member.User.Email) + } + return group +} + +func (superGroups AuthSuperGroups) ToGroups() model.Groups { + groupsList := model.Groups{} + for _, superGroup := range superGroups { + groupsList = append(groupsList, superGroup.ToGroup()) + } + return groupsList +} diff --git a/internal/pkg/services/auth/service.go b/internal/pkg/services/auth/service.go index 7814286..c3c337f 100644 --- a/internal/pkg/services/auth/service.go +++ b/internal/pkg/services/auth/service.go @@ -3,7 +3,7 @@ package auth import ( "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net/http" @@ -15,7 +15,7 @@ type AuthService struct { url string } -//Creates a auth service which has the url to auth and the pre-shared key +// Creates a auth service which has the url to auth and the pre-shared key func CreateAuthService(apiKey string, url string) (AuthService, error) { return AuthService{ apiKey: apiKey, @@ -23,7 +23,7 @@ func CreateAuthService(apiKey string, url string) (AuthService, error) { }, nil } -//Executes a generic get request with api key +// Executes a generic get request with api key func request(s *AuthService, endpoint string, response interface{}) error { req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", s.url, endpoint), nil) if err != nil { @@ -41,7 +41,7 @@ func request(s *AuthService, endpoint string, response interface{}) error { } fmt.Printf("Request sent to: %s [key: %s] status %d\n", endpoint, s.apiKey, resp.StatusCode) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Println(err) return err @@ -57,25 +57,25 @@ func request(s *AuthService, endpoint string, response interface{}) error { } func (s AuthService) GetGroups() ([]model.Group, error) { - var groups []model.Group + var groups AuthSuperGroups - err := request(&s, "/api/goldapps/groups", &groups) + err := request(&s, "/api/account-scaffold/v1/supergroups", &groups) if err != nil { log.Println(err) return nil, err } - return groups, nil + return groups.ToGroups(), nil } func (s AuthService) GetUsers() ([]model.User, error) { - var users []model.User + var users AuthUsers - err := request(&s, "/api/goldapps/users", &users) + err := request(&s, "/api/account-scaffold/v1/users", &users) if err != nil { log.Println(err) return nil, err } - return users, nil + return users.ToUsers(), nil }