Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type SitesToken struct {
APIVersion string `json:"ApiVersion"`
AppID int32 `json:"AppId"`
AppID int64 `json:"AppId"`
AppName string `json:"AppName"`
Authenticated bool `json:"authenticated"`
XSign string `json:"xsign"`
Expand Down
93 changes: 67 additions & 26 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var conf = config.GetInstance()

type LoginResponse struct {
APIVersion string `json:"api_Version"`
AppID int32 `json:"app_id"`
AppID int64 `json:"app_id"`
AppName string `json:"app_name"`
Authenticated bool `json:"authenticated"`
NoOfLogins int32 `json:"no_of_logins"`
Expand All @@ -26,15 +26,25 @@ type LoginResponse struct {
XToken string `json:"xtoken"`
}

func AuthLogin(accessToken string) (*LoginResponse, error) {
type LoginOpts struct {
AccessToken string `schema:"token" json:"accesstoken"`
AppName string `schema:"appName"`
Domain string `schema:"domain" json:"domain"`
DataCenter string `schema:"dataCenter" json:"dataCenter"`
Plan string `schema:"plan" json:"plan"`
Role string `schema:"role" json:"role"`
LookingFor string `schema:"lookingFor" json:"lookingFor"`
}

func AuthLogin(params LoginOpts) (*LoginResponse, error) {

// Admin Console Backend API
var resObj LoginResponse

backendURL := conf.AdminConsoleAPIDomain + "/auth/login"
body, _ := json.Marshal(map[string]string{
"accesstoken": accessToken,
})
if params.AppName != "" {
backendURL += "?appName=" + params.AppName
}
body, _ := json.Marshal(params)
resp, err := request.Rest(http.MethodPost, backendURL, nil, string(body))
if err != nil {
return nil, err
Expand Down Expand Up @@ -67,26 +77,6 @@ func AuthValidateToken() (*ValidateTokenResp, error) {
return &resObj, nil
}

type AppID struct {
CurrentAppId int64 `json:"currentAppId"`
}

func CurrentID() (*AppID, error) {
conf := config.GetInstance()
config := conf.AdminConsoleAPIDomain + "/auth/config?"
var currentAppId AppID
resp, err := request.Rest(http.MethodGet, config, nil, "")
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &currentAppId)
if err != nil {
return nil, err
}

return &currentAppId, nil
}

func SitesBasic(tokens *SitesToken) error {
conf := config.GetInstance()
var newToken SitesToken
Expand Down Expand Up @@ -141,3 +131,54 @@ func SitesBasic(tokens *SitesToken) error {
return nil

}

func GetAppsInfo() (map[int64]SitesReponse, error) {
var Apps CoreAppData
data, err := cmdutil.ReadFile("siteInfo.json")
if err != nil {
coreAppData := conf.AdminConsoleAPIDomain + "/auth/core-app-data?"
data, err = request.Rest(http.MethodGet, coreAppData, nil, "")
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &Apps)
if err != nil {
return nil, err
}
return storeSiteInfo(Apps), nil
}
var siteInfo map[int64]SitesReponse
err = json.Unmarshal(data, &siteInfo)
return siteInfo, nil
}

func CurrentID() (int64, error) {
var loginInfo LoginResponse
data, err := cmdutil.ReadFile("token.json")
if err != nil {
return 0, err
}
err = json.Unmarshal(data, &loginInfo)
if err != nil {
return 0, err
}
return loginInfo.AppID, nil
}

func storeSiteInfo(data CoreAppData) map[int64]SitesReponse {
siteInfo := make(map[int64]SitesReponse, len(data.Apps.Data))
for _, app := range data.Apps.Data {
siteInfo[app.Appid] = app
}
obj, _ := json.Marshal(siteInfo)
cmdutil.WriteFile("siteInfo.json", obj)
currentId, err := CurrentID()
if err == nil {
site, ok := siteInfo[currentId]
if ok {
obj, _ := json.Marshal(site)
cmdutil.WriteFile("currentSite.json", obj)
}
}
return siteInfo
}
38 changes: 0 additions & 38 deletions api/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,44 +129,6 @@ func UpdateDomain(domains string) error {
return nil
}

func GetAppsInfo() (map[int64]SitesReponse, error) {
var Apps CoreAppData
data, err := cmdutil.ReadFile("siteInfo.json")
if err != nil {
coreAppData := conf.AdminConsoleAPIDomain + "/auth/core-app-data?"
data, err = request.Rest(http.MethodGet, coreAppData, nil, "")
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &Apps)
if err != nil {
return nil, err
}
return storeSiteInfo(Apps), nil
}
var siteInfo map[int64]SitesReponse
err = json.Unmarshal(data, &siteInfo)
return siteInfo, nil
}

func storeSiteInfo(data CoreAppData) map[int64]SitesReponse {
siteInfo := make(map[int64]SitesReponse, len(data.Apps.Data))
for _, app := range data.Apps.Data {
siteInfo[app.Appid] = app
}
obj, _ := json.Marshal(siteInfo)
cmdutil.WriteFile("siteInfo.json", obj)
currentId, err := CurrentID()
if err == nil {
site, ok := siteInfo[currentId.CurrentAppId]
if ok {
obj, _ := json.Marshal(site)
cmdutil.WriteFile("currentSite.json", obj)
}
}
return siteInfo
}

func CurrentPlan() error {
sitesResp, err := GetSites()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/delete/site/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func deleteSite() error {
if err != nil {
return err
}
if currentID.CurrentAppId == appid {
if currentID == appid {
fmt.Println("This is the current active site. Please switch to another site before deleting.")
return nil
}
Expand Down
15 changes: 6 additions & 9 deletions cmd/get/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ package domain

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/loginradius/lr-cli/api"
"github.com/spf13/cobra"
)

var fileName string

type domainManagement struct {
CallbackUrl string `json:"CallbackUrl"`
}

var url string

func NewdomainCmd() *cobra.Command {

cmd := &cobra.Command{
Expand All @@ -29,7 +22,11 @@ func NewdomainCmd() *cobra.Command {
if err != nil {
return err
}
fmt.Println(resp.Callbackurl)
res1 := strings.Split(resp.Callbackurl, ";")
for i := 0; i < len(res1); i++ {
fmt.Print(fmt.Sprint(i+1) + ".")
fmt.Println(res1[i])
}
return nil

},
Expand Down
2 changes: 1 addition & 1 deletion cmd/get/site/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func getSite() error {
return err
}
fmt.Println("Active site: ")
val, _ := AppInfo[currentID.CurrentAppId]
val, _ := AppInfo[currentID]
Output(val)
} else if *all && (!*active && *appid == -1) {
fmt.Println("All sites: ")
Expand Down
5 changes: 4 additions & 1 deletion cmd/get/social/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func get() error {
if err != nil {
return err
}
fmt.Println(resultResp)
for i := 0; i < len(resultResp.Data); i++ {
fmt.Print(fmt.Sprint(i+1) + ".")
fmt.Println(resultResp.Data[i].Provider)
}
return nil
}
16 changes: 11 additions & 5 deletions cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package login
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"

Expand Down Expand Up @@ -43,7 +42,7 @@ func NewLoginCmd() *cobra.Command {
if err != nil {
return err
} else if isValid {
log.Printf("%s", "You are already been logged in")
fmt.Printf("%s", "You are already been logged in")
return nil
}
cmdutil.Openbrowser(conf.HubPageDomain + "/auth.aspx?return_url=http://localhost:8089/postLogin")
Expand All @@ -68,17 +67,24 @@ func getAccessToken(w http.ResponseWriter, r *http.Request) {

func doLogin(accessToken string) error {

resObj, err := api.AuthLogin(accessToken)
params := api.LoginOpts{
AccessToken: accessToken,
}
resObj, err := api.AuthLogin(params)
if err != nil {
return err
}
creds, _ := json.Marshal(resObj)
cmdutil.WriteFile("token.json", creds)
err = cmdutil.WriteFile("token.json", creds)
if err != nil {
return err
}
fmt.Println("Successfully Authenticated, Fetching Your Site(s)...")
_, err = api.GetAppsInfo()
if err != nil {
return err
}
log.Println("Successfully Logged In")
fmt.Println("Successfully Logged In")
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/logout/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package logout

import (
"fmt"
"log"
"net/http"
"os"
"os/user"
Expand Down Expand Up @@ -50,7 +49,7 @@ func logout() error {
}

}
log.Println("You are successfully Logged Out")
fmt.Println("You are successfully Logged Out")
return nil
}

Expand Down
30 changes: 23 additions & 7 deletions cmd/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import (
"net/http"
"time"

"github.com/gorilla/schema"
"github.com/loginradius/lr-cli/api"
"github.com/loginradius/lr-cli/cmdutil"
"github.com/loginradius/lr-cli/config"
"github.com/spf13/cobra"
)

var tempServer *cmdutil.TempServer
var tempToken string

var loginparams api.LoginOpts

type ResigterOpts struct {
}

func NewRegisterCmd() *cobra.Command {

Expand All @@ -30,24 +35,35 @@ func NewRegisterCmd() *cobra.Command {
RouteName: "/postLogin",
})
tempServer.Server.ListenAndServe()
return register(tempToken)
return register()
},
}
return cmd
}

func register(token string) error {
resObj, err := api.AuthLogin(token)
func register() error {
resObj, err := api.AuthLogin(loginparams)
if err != nil {
return err
}
fmt.Println("Successfully Registered")
creds, _ := json.Marshal(resObj)
return cmdutil.WriteFile("token.json", creds)
err = cmdutil.WriteFile("token.json", creds)
if err != nil {
return err
}
fmt.Println("Successfully Authenticated, Fetching Your Site(s)...")
_, err = api.GetAppsInfo()
if err != nil {
return err
}
fmt.Println("Successfully Registered")
return nil
}

func getAccessToken(w http.ResponseWriter, r *http.Request) {
tempToken = r.URL.Query().Get("token")
if err := schema.NewDecoder().Decode(&loginparams, r.URL.Query()); err != nil {
fmt.Fprintf(w, err.Error())
}
fmt.Fprintf(w, "You are Successfully Authenticated, Kindly Close this browser window and go back to CLI")
time.AfterFunc(1*time.Second, tempServer.CloseServer)
}
2 changes: 1 addition & 1 deletion cmd/set/site/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func setSite() error {
return err
}
currentID, err := api.CurrentID()
if currentID.CurrentAppId == appid {
if currentID == appid {
fmt.Println("You are already using this site")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/cli/safeexec v1.0.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/schema v1.2.0
github.com/kr/text v0.2.0
github.com/rs/cors v1.7.0
github.com/spf13/cobra v1.1.3
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc
)

replace github.com/loginradius/lr-cli => ../lr-cli
Loading