Skip to content

Commit fd31797

Browse files
authored
Merge pull request #26 from LoginRadius/develop
Merging from Develop to Main for 0.3.0 release
2 parents 100a03d + 2d8d3e4 commit fd31797

47 files changed

Lines changed: 1616 additions & 386 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ $ brew tap loginradius/tap
2525
$ brew install lr
2626
```
2727

28+
### How to Upgrade the loginradius cli using Homebrew?
29+
30+
```sh
31+
$ brew upgrade loginradius/tap/lr
32+
```
33+
2834
## Other Platforms
2935

3036
Download packaged binaries from the [release page](https://github.com/loginradius/lr-cli/releases/latest).

api/account.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"strconv"
7+
"time"
8+
9+
"github.com/loginradius/lr-cli/request"
10+
)
11+
12+
type SitesToken struct {
13+
APIVersion string `json:"ApiVersion"`
14+
AppID int32 `json:"AppId"`
15+
AppName string `json:"AppName"`
16+
Authenticated bool `json:"authenticated"`
17+
XSign string `json:"xsign"`
18+
XToken string `json:"xtoken"`
19+
}
20+
21+
func SetSites(appid int64) (*SitesToken, error) {
22+
switchapp := conf.AdminConsoleAPIDomain + "/account/switchapp?appid=" + strconv.FormatInt(appid, 10)
23+
switchResp, err := request.Rest(http.MethodGet, switchapp, nil, "")
24+
var switchRespObj SitesToken
25+
err = json.Unmarshal(switchResp, &switchRespObj)
26+
if err != nil {
27+
return nil, err
28+
}
29+
return &switchRespObj, nil
30+
}
31+
32+
type AccountPayment struct {
33+
Data struct {
34+
Order []struct {
35+
Totalamount int `json:"TotalAmount"`
36+
Recurringprofileid interface{} `json:"RecurringProfileId"`
37+
Basediscount int `json:"BaseDiscount"`
38+
Promotionaldiscount int `json:"PromotionalDiscount"`
39+
Initialamount int `json:"InitialAmount"`
40+
Tax int `json:"Tax"`
41+
UUID interface{} `json:"Uuid"`
42+
Invoiceno int `json:"InvoiceNo"`
43+
Createddate time.Time `json:"CreatedDate"`
44+
Lastmodifieddate time.Time `json:"LastModifiedDate"`
45+
Isactive bool `json:"IsActive"`
46+
Isdeleted bool `json:"IsDeleted"`
47+
Orderid int `json:"OrderId"`
48+
Paymentdetail struct {
49+
Stripecustomerid string `json:"StripeCustomerId"`
50+
Stripepaymentmethodid string `json:"StripePaymentMethodId"`
51+
} `json:"PaymentDetail"`
52+
Orderdetails []interface{} `json:"OrderDetails"`
53+
} `json:"Order"`
54+
Carddetails struct {
55+
Expmonth int `json:"expMonth"`
56+
Expyear int `json:"expYear"`
57+
Last4 string `json:"last4"`
58+
} `json:"cardDetails"`
59+
} `json:"data"`
60+
Sharedsiteownerdata interface{} `json:"sharedSiteOwnerData"`
61+
}
62+
63+
func PaymentInfo() (*AccountPayment, error) {
64+
payment := conf.AdminConsoleAPIDomain + "/account/accountpaymentdetail?"
65+
paymentResp, err := request.Rest(http.MethodGet, payment, nil, "")
66+
if err != nil {
67+
return nil, err
68+
}
69+
var paymentRespObj AccountPayment
70+
err = json.Unmarshal(paymentResp, &paymentRespObj)
71+
if err != nil {
72+
return nil, err
73+
}
74+
return &paymentRespObj, nil
75+
}

api/auth.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package api
22

33
import (
44
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
58
"net/http"
69
"time"
710

11+
"github.com/loginradius/lr-cli/cmdutil"
812
"github.com/loginradius/lr-cli/config"
913
"github.com/loginradius/lr-cli/request"
1014
)
@@ -62,3 +66,78 @@ func AuthValidateToken() (*ValidateTokenResp, error) {
6266
}
6367
return &resObj, nil
6468
}
69+
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+
90+
func SitesBasic(tokens *SitesToken) error {
91+
conf := config.GetInstance()
92+
var newToken SitesToken
93+
client := &http.Client{}
94+
basic := conf.AdminConsoleAPIDomain + "/auth/basicsettings?"
95+
req, err := http.NewRequest(http.MethodGet, basic, nil)
96+
if err != nil {
97+
log.Printf("Could not make request % -v", err)
98+
}
99+
req.Header.Add("x-is-loginradius--sign", tokens.XSign)
100+
req.Header.Add("x-is-loginradius--token", tokens.XToken)
101+
req.Header.Add("x-is-loginradius-ajax", "true")
102+
req.Header.Add("Content-Type", "application/json; charset=utf-8")
103+
req.Header.Add("Origin", conf.DashboardDomain)
104+
resp, err := client.Do(req)
105+
if err != nil {
106+
fmt.Printf("%s", err.Error())
107+
}
108+
109+
defer resp.Body.Close()
110+
bodyBytes, err := ioutil.ReadAll(resp.Body)
111+
if err != nil {
112+
fmt.Print(err.Error())
113+
}
114+
//obtaining the new tokens
115+
err = json.Unmarshal(bodyBytes, &newToken)
116+
if err != nil {
117+
return err
118+
}
119+
result := LoginResponse{
120+
APIVersion: tokens.APIVersion,
121+
AppName: tokens.AppName,
122+
AppID: tokens.AppID,
123+
Authenticated: true,
124+
XSign: newToken.XSign, //switching tokens
125+
XToken: newToken.XToken,
126+
}
127+
resObj, err := json.Marshal(result)
128+
err = cmdutil.DeleteFiles()
129+
if err != nil {
130+
return err
131+
}
132+
err = cmdutil.WriteFile("token.json", resObj)
133+
if err != nil {
134+
return err
135+
}
136+
_, err = GetAppsInfo()
137+
if err != nil {
138+
return err
139+
}
140+
141+
return nil
142+
143+
}

api/deployment.go

Lines changed: 97 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package api
22

33
import (
44
"encoding/json"
5+
"errors"
6+
57
"net/http"
68
"time"
79

10+
"github.com/loginradius/lr-cli/cmdutil"
811
"github.com/loginradius/lr-cli/request"
912
)
1013

@@ -16,7 +19,7 @@ type SitesReponse struct {
1619
Callbackurl string `json:"CallbackUrl"`
1720
Devdomain string `json:"DevDomain"`
1821
Ismobile bool `json:"IsMobile"`
19-
Appid int `json:"AppId"`
22+
Appid int64 `json:"AppId"`
2023
Key string `json:"Key"`
2124
Secret string `json:"Secret"`
2225
Role string `json:"Role"`
@@ -35,7 +38,7 @@ type SitesReponse struct {
3538
Privacypolicy interface{} `json:"PrivacyPolicy"`
3639
Termsofservice interface{} `json:"TermsOfService"`
3740
Ownerid string `json:"OwnerId"`
38-
Productplan struct {
41+
Productplan *struct {
3942
Name string `json:"Name"`
4043
Expirytime time.Time `json:"ExpiryTime"`
4144
Billingcycle interface{} `json:"BillingCycle"`
@@ -60,20 +63,24 @@ type HostedPageResponse struct {
6063
} `json:"Pages"`
6164
}
6265

63-
func GetSites() (*SitesReponse, error) {
66+
type CoreAppData struct {
67+
Apps struct {
68+
Data []SitesReponse `json:"Data"`
69+
} `json:"apps"`
70+
}
6471

65-
url := conf.AdminConsoleAPIDomain + "/deployment/sites?"
72+
func GetSites() (*SitesReponse, error) {
6673

67-
var resultResp SitesReponse
68-
resp, err := request.Rest(http.MethodGet, url, nil, "")
74+
var siteInfo SitesReponse
75+
data, err := cmdutil.ReadFile("currentSite.json")
6976
if err != nil {
70-
return nil, err
77+
return nil, errors.New("Please Login to execute this command")
7178
}
72-
err = json.Unmarshal(resp, &resultResp)
79+
err = json.Unmarshal(data, &siteInfo)
7380
if err != nil {
7481
return nil, err
7582
}
76-
return &resultResp, nil
83+
return &siteInfo, nil
7784
}
7885

7986
func GetPage() (*HostedPageResponse, error) {
@@ -90,3 +97,84 @@ func GetPage() (*HostedPageResponse, error) {
9097
}
9198
return &resultResp, nil
9299
}
100+
101+
func UpdateDomain(domains string) error {
102+
var url string
103+
body, _ := json.Marshal(map[string]string{
104+
"domain": "http://localhost",
105+
"production": domains,
106+
"staging": "",
107+
})
108+
109+
url = conf.AdminConsoleAPIDomain + "/deployment/sites?"
110+
_, err := request.Rest(http.MethodPost, url, nil, string(body))
111+
if err != nil {
112+
return err
113+
}
114+
115+
// Updating to current site
116+
var siteInfo SitesReponse
117+
data, err := cmdutil.ReadFile("currentSite.json")
118+
if err != nil {
119+
return err
120+
}
121+
err = json.Unmarshal(data, &siteInfo)
122+
if err != nil {
123+
return err
124+
}
125+
siteInfo.Callbackurl = domains
126+
sInfo, _ := json.Marshal(siteInfo)
127+
_ = cmdutil.WriteFile("currentSite.json", sInfo)
128+
129+
return nil
130+
}
131+
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+
170+
func CurrentPlan() error {
171+
sitesResp, err := GetSites()
172+
if err != nil {
173+
return err
174+
}
175+
if sitesResp.Productplan.Name == "free" {
176+
return errors.New("Please switch to an app which enables this feature or upgrade your plan from Free Plan.")
177+
178+
}
179+
return nil
180+
}

api/integration.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"time"
7+
8+
"github.com/loginradius/lr-cli/request"
9+
)
10+
11+
type HooksResponse struct {
12+
Data []struct {
13+
ID string `json:"Id"`
14+
Appid int `json:"AppId"`
15+
Createddate time.Time `json:"CreatedDate"`
16+
Lastmodifieddate time.Time `json:"LastModifiedDate"`
17+
Targeturl string `json:"TargetUrl"`
18+
Event string `json:"Event"`
19+
Name string `json:"Name"`
20+
} `json:"Data"`
21+
}
22+
23+
func Hooks(method string, body string) (*HooksResponse, error) {
24+
hooks := conf.AdminConsoleAPIDomain + "/integrations/webhook?"
25+
resp, err := request.Rest(method, hooks, nil, body)
26+
if err != nil {
27+
return nil, err
28+
}
29+
var resultResp HooksResponse
30+
err = json.Unmarshal(resp, &resultResp)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return &resultResp, nil
35+
}
36+
37+
func CheckHookID(hookid string) (bool, error) {
38+
Hooks, err := Hooks(http.MethodGet, "")
39+
if err != nil {
40+
return false, err
41+
}
42+
for i := 0; i < len(Hooks.Data); i++ {
43+
if hookid == Hooks.Data[i].ID {
44+
return true, nil
45+
}
46+
}
47+
return false, nil
48+
}

0 commit comments

Comments
 (0)