Skip to content

Commit

Permalink
expire android subscriptions, add account.sub
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-nettica committed Jan 14, 2025
1 parent 1713194 commit f50cfe4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 18 deletions.
28 changes: 28 additions & 0 deletions api/v1/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,16 @@ func handleAndroidWebhook(c *gin.Context) {
return
}

if subscription.Status == "expired" {
subscription.Status = "active"
last := time.Now().UTC()
subscription.LastUpdated = &last
subscription.UpdatedBy = "google"
core.RenewSubscription(subscription.Id)
log.Infof("subscription renewed: %s", subscription.Id)
subscription, err = core.GetSubscriptionByReceipt(purchaseToken)
}

// get the lineItems from the subscription. It's an array of maps
lineItems, ok := sub["lineItems"].([]interface{})
if !ok || len(lineItems) == 0 {
Expand Down Expand Up @@ -542,6 +552,24 @@ func handleAndroidWebhook(c *gin.Context) {
return
}
}

if sub["subscriptionState"] != nil && sub["subscriptionState"].(string) == "SUBSCRIPTION_STATE_EXPIRED" {
// retrieve the subscription
subscription, err := core.GetSubscriptionByReceipt(purchaseToken)
if err != nil {
log.Errorf("error getting subscription: %v", err)
c.JSON(http.StatusNotFound, gin.H{"error": "Subscription not found"})
return
}

core.ExpireSubscription(subscription.Id)

log.Infof("subscription expired: %s", subscription.Id)

c.JSON(http.StatusOK, gin.H{"status": "expired"})
return
}

// handle cancel and did_not_renew

}
Expand Down
1 change: 1 addition & 0 deletions auth/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (o *Oauth2Basic) UserInfo(oauth2Token *oauth2.Token) (*model.User, error) {
host, _ := os.Hostname()
account.AccountName = host
account.Name = user.Sub
account.Sub = user.Sub
account.Email = user.Email
account.Role = "Owner"
account.Status = "Active"
Expand Down
1 change: 1 addition & 0 deletions auth/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (o *OAuth2Google) UserInfo(oauth2Token *oauth2.Token) (*model.User, error)
if len(accounts) == 0 {
var account model.Account
account.Name = "Me"
account.Sub = user.Sub
account.AccountName = "Company"
account.Email = user.Email
account.Role = "Owner"
Expand Down
1 change: 1 addition & 0 deletions auth/microsoft/microsoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (o *Oauth2Msft) UserInfo(oauth2Token *oauth2.Token) (*model.User, error) {
if len(accounts) == 0 {
var account model.Account
account.Name = "Me"
account.Sub = user.Sub
account.AccountName = "Company"
account.Email = user.Email
account.Role = "Owner"
Expand Down
1 change: 1 addition & 0 deletions auth/microsoft2/microsoft2.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func (o *Oauth2Microsoft) UserInfo(oauth2Token *oauth2.Token) (*model.User, erro
if len(accounts) == 0 {
var account model.Account
account.Name = "Me"
account.Sub = user.Sub
account.AccountName = "Company"
account.Email = user.Email
account.Role = "Owner"
Expand Down
10 changes: 10 additions & 0 deletions auth/oauth2oidc/oauth2oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func (o *Oauth2idc) UserInfo(oauth2Token *oauth2.Token) (*model.User, error) {
}
account.AccountName = "Company"
account.Email = user.Email
account.Sub = user.Sub
account.Role = "Owner"
account.Status = "Active"
account.CreatedBy = user.Email
Expand Down Expand Up @@ -324,6 +325,15 @@ func (o *Oauth2idc) UserInfo(oauth2Token *oauth2.Token) (*model.User, error) {
mongodb.Serialize(limits_id, "id", "limits", limits)
}
}

// add sub to existing accounts missing it
for i := 0; i < len(accounts); i++ {
if accounts[i].Sub == "" {
accounts[i].Sub = user.Sub
core.UpdateAccount(accounts[i].Id, accounts[i])
}
}

for i := 0; i < len(accounts); i++ {
if accounts[i].Picture == "" {
accounts[i].Picture = user.Picture
Expand Down
37 changes: 19 additions & 18 deletions model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ import (
)

type Account struct {
Id string `json:"id" bson:"id"`
Parent string `json:"parent" bson:"parent"`
Email string `json:"email" bson:"email"`
Name string `json:"name" bson:"name"`
AccountName string `json:"accountName" bson:"accountName"`
NetId string `json:"netId" bson:"netId"`
NetName string `json:"netName" bson:"netName"`
Picture string `json:"picture" bson:"picture"`
Role string `json:"role" bson:"role"`
Status string `json:"status" bson:"status"`
ApiKey string `json:"apiKey" bson:"apiKey"`
CreatedBy string `json:"createdBy" bson:"createdBy"`
UpdatedBy string `json:"updatedBy" bson:"updatedBy"`
Created time.Time `json:"created" bson:"created"`
Updated time.Time `json:"updated" bson:"updated"`
Networks []*Network `json:"networks" bson:"networks"`
VPNs []*VPN `json:"vpns" bson:"vpns"`
Devices []*Device `json:"devices" bson:"devices"`
Id string `json:"id" bson:"id"`
Parent string `json:"parent" bson:"parent"`
Email string `json:"email" bson:"email"`
Sub string `json:"sub,omitempty" bson:"sub,omitempty"`
Name string `json:"name" bson:"name"`
AccountName string `json:"accountName" bson:"accountName"`
NetId string `json:"netId" bson:"netId"`
NetName string `json:"netName" bson:"netName"`
Picture string `json:"picture" bson:"picture"`
Role string `json:"role" bson:"role"`
Status string `json:"status" bson:"status"`
ApiKey string `json:"apiKey" bson:"apiKey"`
CreatedBy string `json:"createdBy" bson:"createdBy"`
UpdatedBy string `json:"updatedBy" bson:"updatedBy"`
Created time.Time `json:"created" bson:"created"`
Updated time.Time `json:"updated" bson:"updated"`
Networks []*Network `json:"networks" bson:"networks"`
VPNs []*VPN `json:"vpns" bson:"vpns"`
Devices []*Device `json:"devices" bson:"devices"`
}

// IsValid check if model is valid
Expand Down

0 comments on commit f50cfe4

Please sign in to comment.