Skip to content

Commit

Permalink
Merge pull request #6 from doneill/feature/jdo-23-user-data
Browse files Browse the repository at this point in the history
user command impl
  • Loading branch information
doneill committed Nov 29, 2023
2 parents 44664d4 + 81cd952 commit 35fc92f
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 90 deletions.
53 changes: 53 additions & 0 deletions api/apiauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package api

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)

// ----------------------------------------------
// stucts
// ----------------------------------------------

type AuthResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
RefreshToken string `json:"refresh_token"`
ErrorDescription string `json:"error_description"`
}

// ----------------------------------------------
// exported funtions
// ----------------------------------------------

func Authenticate(sitename, username, password string) (*AuthResponse, error) {
client := &http.Client{}
authReq := getAuthRequest(sitename)
authReq.Body = io.NopCloser(strings.NewReader(fmt.Sprintf("username=%s&password=%s&client_id=er_mobile_tracker&grant_type=password", username, password)))

res, err := client.Do(&authReq)
if err != nil {
fmt.Println("Error making request:", err)
}
defer res.Body.Close()

var responseData AuthResponse
err = json.NewDecoder(res.Body).Decode(&responseData)
if err != nil {
fmt.Println("Error decoding response:", err)
}

if res.StatusCode == 200 {
return &responseData, nil
}

fmt.Println("Error:", res.StatusCode)
fmt.Println("Error Description:", responseData.ErrorDescription)

return nil, err
}
70 changes: 70 additions & 0 deletions api/apiuser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package api

import (
"encoding/json"
"fmt"
"net/http"

"github.com/doneill/er-cli-go/config"
)

// ----------------------------------------------
// stucts
// ----------------------------------------------

type UserResponse struct {
Data struct {
Username string `json:"username"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Role string `json:"role"`
IsStaff bool `json:"is_staff"`
IsSuperUser bool `json:"is_superuser"`
DateJoined string `json:"date_joined"`
ID string `json:"id"`
IsActive bool `json:"is_active"`
LastLogin string `json:"last_login"`
Pin string `json:"pin"`
Subject struct {
ID string `json:"id"`
} `json:"subject"`
Permissions struct {
Patrol []string `json:"patrol"`
MobileTests []string `json:"mobile_tests"`
} `json:"permissions"`
} `json:"data"`
ErrorDescription string `json:"error_description"`
Status struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"status"`
}

// ----------------------------------------------
// exported funtions
// ----------------------------------------------

func User() (*UserResponse, error) {
client := &http.Client{}
clientReq := getClientRequest(config.Sitename(), API_USER_ME, config.Token())

res, err := client.Do(&clientReq)
if err != nil {
fmt.Println("Error making request:", err)
}
var responseData UserResponse
err = json.NewDecoder(res.Body).Decode(&responseData)
if err != nil {
fmt.Println("Error decoding response:", err)
}

if res.StatusCode == 200 {
return &responseData, nil
}

fmt.Println("Error:", res.StatusCode)
fmt.Println("Error Description:", responseData.ErrorDescription)

return nil, err
}
74 changes: 0 additions & 74 deletions api/earthranger_service.go

This file was deleted.

50 changes: 50 additions & 0 deletions api/ersvc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package api

import (
"fmt"
"net/http"
)

// ----------------------------------------------
// const endpoints
// ----------------------------------------------

const DOMAIN = ".pamdas.org"

const API_V1 = "/api/v1.0"

const API_AUTH = "/oauth2/token"

const API_USER = API_V1 + "/user"

const API_USER_ME = API_USER + "/me"

// ----------------------------------------------
// funtions
// ----------------------------------------------

func getApiUrl(sitename string, endpoint string) string {
return fmt.Sprintf("https://%s%s%s", sitename, DOMAIN, endpoint)
}

func getAuthRequest(sitename string) http.Request {
req, err := http.NewRequest("POST", getApiUrl(sitename, API_AUTH), nil)
if err != nil {
fmt.Println("Error creating request:", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")

return *req
}

func getClientRequest(sitename string, endpoint string, token string) http.Request {
req, err := http.NewRequest("GET", getApiUrl(sitename, endpoint), nil)
if err != nil {
fmt.Println("Error creating request:", err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Cache-control", "no-cache")

return *req
}
11 changes: 6 additions & 5 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var authCmd = &cobra.Command{
}

// ----------------------------------------------
// funtions
// functions
// ----------------------------------------------

func auth() {
Expand All @@ -43,17 +43,18 @@ func auth() {
}

// Call the authenticate function to get the access token and expires in
response, err := api.Authenticate(SITENAME, USERNAME, password)
authResponse, err := api.Authenticate(SITENAME, USERNAME, password)
if err != nil {
fmt.Println("Error authenticating:", err)
os.Exit(1)
}

// Print out the access token and expires in if the request was successful
if response != nil {
if authResponse != nil {
viper.Set("user", USERNAME)
viper.Set("oauth_token", response.AccessToken)
viper.Set("expires", response.ExpiresIn)
viper.Set("sitename", SITENAME)
viper.Set("oauth_token", authResponse.AccessToken)
viper.Set("expires", authResponse.ExpiresIn)
err := viper.WriteConfigAs(PROGRAM_NAME + CONFIG_TYPE)
if err != nil {
fmt.Println("Error writing configuration file:", err)
Expand Down
14 changes: 3 additions & 11 deletions cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cmd
import (
"fmt"

"github.com/doneill/er-cli-go/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// ----------------------------------------------
Expand All @@ -25,16 +25,8 @@ var tokenCmd = &cobra.Command{
// ----------------------------------------------

func token() {
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("Error: er not configured properly, try reauthenticating")
} else {
fmt.Println("Error:", err)
}
} else {
var token = viper.Get("oauth_token")
fmt.Println(token)
}
var token = config.Token()
fmt.Println(token)
}

// ----------------------------------------------
Expand Down
62 changes: 62 additions & 0 deletions cmd/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"fmt"
"os"

"github.com/doneill/er-cli-go/api"
"github.com/spf13/cobra"
)

// ----------------------------------------------
// user command
// ----------------------------------------------

var userCmd = &cobra.Command{
Use: "user",
Short: "Current authenticated user data",
Long: `Return the currently authenticated er user data`,
Run: func(cmd *cobra.Command, args []string) {
user()
},
}

// ----------------------------------------------
// functions
// ----------------------------------------------

func user() {
userResponse, err := api.User()
if err != nil {
fmt.Println("Error authenticating:", err)
os.Exit(1)
}

if userResponse != nil {
formattedResponse := fmt.Sprintf("username: %s\nemail: %s\nfirst name: %s\nlast name: %s\nrole: %s\nis staff: %t\nis superuser: %t\ndate joined: %s\nid: %s\nisactive: %t\nlast login: %s\npin: %s\nsubject id: %s\npermissions:\n patrol: %v\nmobile tests: %v",
userResponse.Data.Username,
userResponse.Data.Email,
userResponse.Data.FirstName,
userResponse.Data.LastName,
userResponse.Data.Role,
userResponse.Data.IsStaff,
userResponse.Data.IsSuperUser,
userResponse.Data.DateJoined,
userResponse.Data.ID,
userResponse.Data.IsActive,
userResponse.Data.LastLogin,
userResponse.Data.Pin,
userResponse.Data.Subject.ID,
userResponse.Data.Permissions.Patrol,
userResponse.Data.Permissions.MobileTests)
fmt.Println(formattedResponse)
}
}

// ----------------------------------------------
// initialize
// ----------------------------------------------

func init() {
rootCmd.AddCommand(userCmd)
}
Loading

0 comments on commit 35fc92f

Please sign in to comment.