Skip to content

Commit

Permalink
Use new auth models
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscariremma committed Jun 8, 2024
1 parent 83d69b9 commit 8e2a396
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
70 changes: 70 additions & 0 deletions internal/pkg/services/auth/models.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 10 additions & 10 deletions internal/pkg/services/auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package auth
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"

Expand All @@ -15,15 +15,15 @@ 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,
url: url,
}, 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 {
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 8e2a396

Please sign in to comment.