Skip to content

Commit c92a000

Browse files
authored
Merge pull request #29 from LoginRadius/develop
Release: 0.3.1
2 parents fd31797 + 3792813 commit c92a000

13 files changed

Lines changed: 119 additions & 94 deletions

File tree

api/account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
type SitesToken struct {
1313
APIVersion string `json:"ApiVersion"`
14-
AppID int32 `json:"AppId"`
14+
AppID int64 `json:"AppId"`
1515
AppName string `json:"AppName"`
1616
Authenticated bool `json:"authenticated"`
1717
XSign string `json:"xsign"`

api/auth.go

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var conf = config.GetInstance()
1717

1818
type LoginResponse struct {
1919
APIVersion string `json:"api_Version"`
20-
AppID int32 `json:"app_id"`
20+
AppID int64 `json:"app_id"`
2121
AppName string `json:"app_name"`
2222
Authenticated bool `json:"authenticated"`
2323
NoOfLogins int32 `json:"no_of_logins"`
@@ -26,15 +26,25 @@ type LoginResponse struct {
2626
XToken string `json:"xtoken"`
2727
}
2828

29-
func AuthLogin(accessToken string) (*LoginResponse, error) {
29+
type LoginOpts struct {
30+
AccessToken string `schema:"token" json:"accesstoken"`
31+
AppName string `schema:"appName"`
32+
Domain string `schema:"domain" json:"domain"`
33+
DataCenter string `schema:"dataCenter" json:"dataCenter"`
34+
Plan string `schema:"plan" json:"plan"`
35+
Role string `schema:"role" json:"role"`
36+
LookingFor string `schema:"lookingFor" json:"lookingFor"`
37+
}
38+
39+
func AuthLogin(params LoginOpts) (*LoginResponse, error) {
3040

31-
// Admin Console Backend API
3241
var resObj LoginResponse
3342

3443
backendURL := conf.AdminConsoleAPIDomain + "/auth/login"
35-
body, _ := json.Marshal(map[string]string{
36-
"accesstoken": accessToken,
37-
})
44+
if params.AppName != "" {
45+
backendURL += "?appName=" + params.AppName
46+
}
47+
body, _ := json.Marshal(params)
3848
resp, err := request.Rest(http.MethodPost, backendURL, nil, string(body))
3949
if err != nil {
4050
return nil, err
@@ -67,26 +77,6 @@ func AuthValidateToken() (*ValidateTokenResp, error) {
6777
return &resObj, nil
6878
}
6979

70-
type AppID struct {
71-
CurrentAppId int64 `json:"currentAppId"`
72-
}
73-
74-
func CurrentID() (*AppID, error) {
75-
conf := config.GetInstance()
76-
config := conf.AdminConsoleAPIDomain + "/auth/config?"
77-
var currentAppId AppID
78-
resp, err := request.Rest(http.MethodGet, config, nil, "")
79-
if err != nil {
80-
return nil, err
81-
}
82-
err = json.Unmarshal(resp, &currentAppId)
83-
if err != nil {
84-
return nil, err
85-
}
86-
87-
return &currentAppId, nil
88-
}
89-
9080
func SitesBasic(tokens *SitesToken) error {
9181
conf := config.GetInstance()
9282
var newToken SitesToken
@@ -141,3 +131,54 @@ func SitesBasic(tokens *SitesToken) error {
141131
return nil
142132

143133
}
134+
135+
func GetAppsInfo() (map[int64]SitesReponse, error) {
136+
var Apps CoreAppData
137+
data, err := cmdutil.ReadFile("siteInfo.json")
138+
if err != nil {
139+
coreAppData := conf.AdminConsoleAPIDomain + "/auth/core-app-data?"
140+
data, err = request.Rest(http.MethodGet, coreAppData, nil, "")
141+
if err != nil {
142+
return nil, err
143+
}
144+
err = json.Unmarshal(data, &Apps)
145+
if err != nil {
146+
return nil, err
147+
}
148+
return storeSiteInfo(Apps), nil
149+
}
150+
var siteInfo map[int64]SitesReponse
151+
err = json.Unmarshal(data, &siteInfo)
152+
return siteInfo, nil
153+
}
154+
155+
func CurrentID() (int64, error) {
156+
var loginInfo LoginResponse
157+
data, err := cmdutil.ReadFile("token.json")
158+
if err != nil {
159+
return 0, err
160+
}
161+
err = json.Unmarshal(data, &loginInfo)
162+
if err != nil {
163+
return 0, err
164+
}
165+
return loginInfo.AppID, nil
166+
}
167+
168+
func storeSiteInfo(data CoreAppData) map[int64]SitesReponse {
169+
siteInfo := make(map[int64]SitesReponse, len(data.Apps.Data))
170+
for _, app := range data.Apps.Data {
171+
siteInfo[app.Appid] = app
172+
}
173+
obj, _ := json.Marshal(siteInfo)
174+
cmdutil.WriteFile("siteInfo.json", obj)
175+
currentId, err := CurrentID()
176+
if err == nil {
177+
site, ok := siteInfo[currentId]
178+
if ok {
179+
obj, _ := json.Marshal(site)
180+
cmdutil.WriteFile("currentSite.json", obj)
181+
}
182+
}
183+
return siteInfo
184+
}

api/deployment.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -129,44 +129,6 @@ func UpdateDomain(domains string) error {
129129
return nil
130130
}
131131

132-
func GetAppsInfo() (map[int64]SitesReponse, error) {
133-
var Apps CoreAppData
134-
data, err := cmdutil.ReadFile("siteInfo.json")
135-
if err != nil {
136-
coreAppData := conf.AdminConsoleAPIDomain + "/auth/core-app-data?"
137-
data, err = request.Rest(http.MethodGet, coreAppData, nil, "")
138-
if err != nil {
139-
return nil, err
140-
}
141-
err = json.Unmarshal(data, &Apps)
142-
if err != nil {
143-
return nil, err
144-
}
145-
return storeSiteInfo(Apps), nil
146-
}
147-
var siteInfo map[int64]SitesReponse
148-
err = json.Unmarshal(data, &siteInfo)
149-
return siteInfo, nil
150-
}
151-
152-
func storeSiteInfo(data CoreAppData) map[int64]SitesReponse {
153-
siteInfo := make(map[int64]SitesReponse, len(data.Apps.Data))
154-
for _, app := range data.Apps.Data {
155-
siteInfo[app.Appid] = app
156-
}
157-
obj, _ := json.Marshal(siteInfo)
158-
cmdutil.WriteFile("siteInfo.json", obj)
159-
currentId, err := CurrentID()
160-
if err == nil {
161-
site, ok := siteInfo[currentId.CurrentAppId]
162-
if ok {
163-
obj, _ := json.Marshal(site)
164-
cmdutil.WriteFile("currentSite.json", obj)
165-
}
166-
}
167-
return siteInfo
168-
}
169-
170132
func CurrentPlan() error {
171133
sitesResp, err := GetSites()
172134
if err != nil {

cmd/delete/site/site.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func deleteSite() error {
6060
if err != nil {
6161
return err
6262
}
63-
if currentID.CurrentAppId == appid {
63+
if currentID == appid {
6464
fmt.Println("This is the current active site. Please switch to another site before deleting.")
6565
return nil
6666
}

cmd/get/domain/domain.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@ package domain
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/MakeNowJust/heredoc"
78
"github.com/loginradius/lr-cli/api"
89
"github.com/spf13/cobra"
910
)
1011

11-
var fileName string
12-
13-
type domainManagement struct {
14-
CallbackUrl string `json:"CallbackUrl"`
15-
}
16-
17-
var url string
18-
1912
func NewdomainCmd() *cobra.Command {
2013

2114
cmd := &cobra.Command{
@@ -29,7 +22,11 @@ func NewdomainCmd() *cobra.Command {
2922
if err != nil {
3023
return err
3124
}
32-
fmt.Println(resp.Callbackurl)
25+
res1 := strings.Split(resp.Callbackurl, ";")
26+
for i := 0; i < len(res1); i++ {
27+
fmt.Print(fmt.Sprint(i+1) + ".")
28+
fmt.Println(res1[i])
29+
}
3330
return nil
3431

3532
},

cmd/get/site/site.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func getSite() error {
6060
return err
6161
}
6262
fmt.Println("Active site: ")
63-
val, _ := AppInfo[currentID.CurrentAppId]
63+
val, _ := AppInfo[currentID]
6464
Output(val)
6565
} else if *all && (!*active && *appid == -1) {
6666
fmt.Println("All sites: ")

cmd/get/social/social.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func get() error {
5454
if err != nil {
5555
return err
5656
}
57-
fmt.Println(resultResp)
57+
for i := 0; i < len(resultResp.Data); i++ {
58+
fmt.Print(fmt.Sprint(i+1) + ".")
59+
fmt.Println(resultResp.Data[i].Provider)
60+
}
5861
return nil
5962
}

cmd/login/login.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package login
33
import (
44
"encoding/json"
55
"fmt"
6-
"log"
76
"net/http"
87
"time"
98

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

6968
func doLogin(accessToken string) error {
7069

71-
resObj, err := api.AuthLogin(accessToken)
70+
params := api.LoginOpts{
71+
AccessToken: accessToken,
72+
}
73+
resObj, err := api.AuthLogin(params)
7274
if err != nil {
7375
return err
7476
}
7577
creds, _ := json.Marshal(resObj)
76-
cmdutil.WriteFile("token.json", creds)
78+
err = cmdutil.WriteFile("token.json", creds)
79+
if err != nil {
80+
return err
81+
}
82+
fmt.Println("Successfully Authenticated, Fetching Your Site(s)...")
7783
_, err = api.GetAppsInfo()
7884
if err != nil {
7985
return err
8086
}
81-
log.Println("Successfully Logged In")
87+
fmt.Println("Successfully Logged In")
8288
return nil
8389
}
8490

cmd/logout/logout.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package logout
22

33
import (
44
"fmt"
5-
"log"
65
"net/http"
76
"os"
87
"os/user"
@@ -50,7 +49,7 @@ func logout() error {
5049
}
5150

5251
}
53-
log.Println("You are successfully Logged Out")
52+
fmt.Println("You are successfully Logged Out")
5453
return nil
5554
}
5655

cmd/register/register.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ import (
66
"net/http"
77
"time"
88

9+
"github.com/gorilla/schema"
910
"github.com/loginradius/lr-cli/api"
1011
"github.com/loginradius/lr-cli/cmdutil"
1112
"github.com/loginradius/lr-cli/config"
1213
"github.com/spf13/cobra"
1314
)
1415

1516
var tempServer *cmdutil.TempServer
16-
var tempToken string
17+
18+
var loginparams api.LoginOpts
19+
20+
type ResigterOpts struct {
21+
}
1722

1823
func NewRegisterCmd() *cobra.Command {
1924

@@ -30,24 +35,35 @@ func NewRegisterCmd() *cobra.Command {
3035
RouteName: "/postLogin",
3136
})
3237
tempServer.Server.ListenAndServe()
33-
return register(tempToken)
38+
return register()
3439
},
3540
}
3641
return cmd
3742
}
3843

39-
func register(token string) error {
40-
resObj, err := api.AuthLogin(token)
44+
func register() error {
45+
resObj, err := api.AuthLogin(loginparams)
4146
if err != nil {
4247
return err
4348
}
44-
fmt.Println("Successfully Registered")
4549
creds, _ := json.Marshal(resObj)
46-
return cmdutil.WriteFile("token.json", creds)
50+
err = cmdutil.WriteFile("token.json", creds)
51+
if err != nil {
52+
return err
53+
}
54+
fmt.Println("Successfully Authenticated, Fetching Your Site(s)...")
55+
_, err = api.GetAppsInfo()
56+
if err != nil {
57+
return err
58+
}
59+
fmt.Println("Successfully Registered")
60+
return nil
4761
}
4862

4963
func getAccessToken(w http.ResponseWriter, r *http.Request) {
50-
tempToken = r.URL.Query().Get("token")
64+
if err := schema.NewDecoder().Decode(&loginparams, r.URL.Query()); err != nil {
65+
fmt.Fprintf(w, err.Error())
66+
}
5167
fmt.Fprintf(w, "You are Successfully Authenticated, Kindly Close this browser window and go back to CLI")
5268
time.AfterFunc(1*time.Second, tempServer.CloseServer)
5369
}

0 commit comments

Comments
 (0)