Skip to content

Commit 082dfc0

Browse files
committed
Auth user endpoint
1 parent bbb32d6 commit 082dfc0

File tree

6 files changed

+99
-2
lines changed

6 files changed

+99
-2
lines changed

cmd/bugout/brood/user.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func CreateUserCommand() *cobra.Command {
1717
Short: "Bugout user operations",
1818
}
1919

20+
userAuthCmd := CreateUserAuthCommand()
2021
userCreateCmd := CreateUserCreateCommand()
2122
userLoginCmd := CreateUserLoginCommand()
2223
userTokensCmd := CreateUserTokensCommand()
@@ -25,11 +26,38 @@ func CreateUserCommand() *cobra.Command {
2526
userVerifyCmd := CreateUserVerifyCommand()
2627
userChangePasswordCmd := CreateUserChangePasswordCommand()
2728

28-
userCmd.AddCommand(userCreateCmd, userLoginCmd, userTokensCmd, userGetCmd, userFindCmd, userVerifyCmd, userChangePasswordCmd)
29+
userCmd.AddCommand(userAuthCmd, userCreateCmd, userLoginCmd, userTokensCmd, userGetCmd, userFindCmd, userVerifyCmd, userChangePasswordCmd)
2930

3031
return userCmd
3132
}
3233

34+
func CreateUserAuthCommand() *cobra.Command {
35+
var token string
36+
userGetCmd := &cobra.Command{
37+
Use: "auth",
38+
Short: "Get the user with groups represented by a token",
39+
PreRunE: cmdutils.TokenArgPopulator,
40+
RunE: func(cmd *cobra.Command, args []string) error {
41+
client, err := bugout.ClientFromEnv()
42+
if err != nil {
43+
return err
44+
}
45+
46+
userAuth, err := client.Brood.Auth(token)
47+
if err != nil {
48+
return err
49+
}
50+
51+
encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(&userAuth)
52+
return encodeErr
53+
},
54+
}
55+
56+
userGetCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request")
57+
58+
return userGetCmd
59+
}
60+
3361
func CreateUserCreateCommand() *cobra.Command {
3462
var username, email, password string
3563
userCreateCmd := &cobra.Command{

dev.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env sh
2+
3+
# Compile application and run with provided arguments
4+
set -e
5+
6+
PROGRAM_NAME="bugout"
7+
8+
go build -o "$PROGRAM_NAME" ./cmd/bugout
9+
10+
./"$PROGRAM_NAME" "$@"

pkg/brood/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const BugoutBroodURL string = "https://auth.bugout.dev"
1818
type BroodCaller interface {
1919
Ping() (string, error)
2020
Version() (string, error)
21+
Auth(token string) (AuthUser, error)
2122
CreateUser(string, string, string) (User, error)
2223
GenerateToken(string, string) (string, error)
2324
AnnotateToken(token, tokenType, note string) (string, error)
@@ -49,6 +50,7 @@ type BroodCaller interface {
4950
type BroodRoutes struct {
5051
Ping string
5152
Version string
53+
Auth string
5254
User string
5355
FindUser string
5456
Groups string
@@ -69,6 +71,7 @@ func RoutesFromURL(broodURL string) BroodRoutes {
6971
return BroodRoutes{
7072
Ping: fmt.Sprintf("%s/ping", cleanURL),
7173
Version: fmt.Sprintf("%s/version", cleanURL),
74+
Auth: fmt.Sprintf("%s/auth", cleanURL),
7275
User: fmt.Sprintf("%s/user", cleanURL),
7376
FindUser: fmt.Sprintf("%s/user/find", cleanURL),
7477
Groups: fmt.Sprintf("%s/groups", cleanURL),

pkg/brood/data.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,36 @@ import (
44
"encoding/json"
55
)
66

7+
type AuthUserGroup struct {
8+
GroupId string `json:"group_id"`
9+
UserId string `json:"user_id"`
10+
UserType string `json:"user_type"`
11+
Autogenerated bool `json:"autogenerated"`
12+
GroupName string `json:"group_name"`
13+
}
14+
15+
type AuthUser struct {
16+
UserId string `json:"user_id"`
17+
Username string `json:"username"`
18+
Email string `json:"email"`
19+
NormalizedEmail string `json:"normalized_email"`
20+
Verified bool `json:"verified"`
21+
Autogenerated bool `json:"autogenerated"`
22+
ApplicationId string `json:"application_id"`
23+
CreatedAt string `json:"created_at"`
24+
UpdatedAt string `json:"updated_at"`
25+
26+
Groups []AuthUserGroup `json:"groups"`
27+
}
28+
729
type User struct {
830
Id string `json:"id"`
931
Username string `json:"username"`
1032
Email string `json:"email"`
1133
NormalizedEmail string `json:"normalized_email"`
1234
Verified bool `json:"verified"`
35+
ApplicationId string `json:"application_id"`
36+
Autogenerated bool `json:"autogenerated"`
1337
CreatedAt string `json:"created_at"`
1438
UpdatedAt string `json:"updated_at"`
1539
}

pkg/brood/user.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,38 @@ func getUserID(decoder *json.Decoder) (string, error) {
2222
return userIDWrapper.UserID, decodeErr
2323
}
2424

25+
func (client BroodClient) Auth(token string) (AuthUser, error) {
26+
authRoute := client.Routes.Auth
27+
request, requestErr := http.NewRequest("GET", authRoute, nil)
28+
if requestErr != nil {
29+
return AuthUser{}, requestErr
30+
}
31+
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
32+
request.Header.Add("Accept", "application/json")
33+
34+
response, err := client.HTTPClient.Do(request)
35+
if err != nil {
36+
return AuthUser{}, err
37+
}
38+
defer response.Body.Close()
39+
40+
var buf bytes.Buffer
41+
bodyReader := io.TeeReader(response.Body, &buf)
42+
43+
statusErr := utils.HTTPStatusCheck(response)
44+
if statusErr != nil {
45+
return AuthUser{}, statusErr
46+
}
47+
48+
var authUser AuthUser
49+
decodeErr := json.NewDecoder(bodyReader).Decode(&authUser)
50+
if decodeErr != nil {
51+
return authUser, decodeErr
52+
}
53+
54+
return authUser, nil
55+
}
56+
2557
func (client BroodClient) CreateUser(username, email, password string) (User, error) {
2658
userRoute := client.Routes.User
2759
data := url.Values{}

pkg/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package bugout
22

3-
const Version string = "0.4.6"
3+
const Version string = "0.4.7"

0 commit comments

Comments
 (0)