Skip to content

Commit 83b1b30

Browse files
committed
fix push chatter
1 parent b129229 commit 83b1b30

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

api/v1/auth/auth.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func oauth2URL(c *gin.Context) {
101101
Redirect: redirect_uri,
102102
}
103103

104-
log.Infof( "model.Auth = %v", data );
104+
log.Infof("model.Auth = %v", data)
105105

106106
c.JSON(http.StatusOK, data)
107107
}
@@ -317,17 +317,16 @@ func login(c *gin.Context) {
317317
if loginVals.Redirect == "com.nettica.agent://callback/agent" {
318318

319319
c.Redirect(http.StatusPermanentRedirect, redirect)
320-
return;
320+
return
321321
}
322322
}
323323

324324
// otherwise send a JSON body with the result to the browser. it will do the redirect.
325-
loginVals.Redirect = redirect;
325+
loginVals.Redirect = redirect
326326

327-
c.JSON( http.StatusOK, loginVals )
327+
c.JSON(http.StatusOK, loginVals)
328328
}
329329

330-
331330
func validate(c *gin.Context) {
332331
var t model.OAuth2Token
333332
if err := c.ShouldBindJSON(&t); err != nil {
@@ -453,9 +452,36 @@ func logout(c *gin.Context) {
453452

454453
func user(c *gin.Context) {
455454
cacheDb := c.MustGet("cache").(*cache.Cache)
456-
oauth2Token, exists := cacheDb.Get(util.GetCleanAuthToken(c))
455+
token := util.GetCleanAuthToken(c)
456+
oauth2Token, exists := cacheDb.Get(token)
457+
id_token := c.Request.Header.Get("X-OAUTH2-ID-TOKEN")
457458

458-
if exists && oauth2Token.(*oauth2.Token).AccessToken == util.GetCleanAuthToken(c) {
459+
if id_token != "" {
460+
new_token := &oauth2.Token{
461+
AccessToken: token,
462+
TokenType: "Bearer",
463+
RefreshToken: "",
464+
Expiry: time.Now().Add(time.Hour * 24),
465+
}
466+
m := make(map[string]interface{})
467+
m["id_token"] = id_token
468+
new_token = new_token.WithExtra(m)
469+
470+
// check if token is valid
471+
var err error
472+
oauth2Token, err = util.ValidateToken(new_token.AccessToken)
473+
if err != nil {
474+
log.WithFields(log.Fields{
475+
"err": err,
476+
"token": oauth2Token,
477+
}).Error("failed to get token info")
478+
c.AbortWithStatus(http.StatusUnauthorized)
479+
return
480+
}
481+
oauth2Token = new_token
482+
}
483+
484+
if id_token != "" || (exists && oauth2Token.(*oauth2.Token).AccessToken == util.GetCleanAuthToken(c)) {
459485
oauth2Client := c.MustGet("oauth2Client").(model.Authentication)
460486

461487
user, err := oauth2Client.UserInfo(oauth2Token.(*oauth2.Token))

api/v1/vpn/vpn.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func createVPN(c *gin.Context) {
105105
core.FlushCache(v.DeviceID)
106106

107107
// send push notification if appropriate
108-
if push.PushDevices[v.DeviceID] != "" {
108+
if push.PushDevices[v.DeviceID] != "" && v.Enable {
109109
err := push.SendPushNotification(push.PushDevices[v.DeviceID], v.NetName+" updated", "The VPN configuration for "+v.NetName+" has been updated")
110110
if err != nil {
111111
log.WithFields(log.Fields{
@@ -313,7 +313,7 @@ func updateVPN(c *gin.Context) {
313313
core.FlushCache(v.DeviceID)
314314

315315
// send push notification if appropriate
316-
if push.PushDevices[v.DeviceID] != "" {
316+
if push.PushDevices[v.DeviceID] != "" && v.Enable {
317317
err := push.SendPushNotification(push.PushDevices[v.DeviceID], v.NetName+" updated", "The VPN configuration for "+v.NetName+" has been updated")
318318
if err != nil {
319319
log.WithFields(log.Fields{
@@ -426,7 +426,7 @@ func deleteVPN(c *gin.Context) {
426426
core.FlushCache(v.DeviceID)
427427

428428
// send push notification if appropriate
429-
if push.PushDevices[v.DeviceID] != "" {
429+
if push.PushDevices[v.DeviceID] != "" && v.Enable {
430430
err := push.SendPushNotification(push.PushDevices[v.DeviceID], v.NetName+" updated", "The VPN configuration for "+v.NetName+" has been updated")
431431
if err != nil {
432432
log.WithFields(log.Fields{

core/device.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
model "github.com/nettica-com/nettica-admin/model"
1111
mongo "github.com/nettica-com/nettica-admin/mongo"
12+
"github.com/nettica-com/nettica-admin/push"
1213
util "github.com/nettica-com/nettica-admin/util"
1314
log "github.com/sirupsen/logrus"
1415
)
@@ -160,7 +161,25 @@ func UpdateDevice(Id string, device *model.Device, fUpdated bool) (*model.Device
160161
}
161162
}
162163

163-
current.Push = device.Push
164+
// current is the old value, device is the new value
165+
if current.Push == "" {
166+
// if the new value is not empty, add it to the push list
167+
if device.Push != "" {
168+
push.PushDevices[device.Id] = device.Push
169+
}
170+
current.Push = device.Push
171+
// here we drop out of the if when both were empty
172+
173+
} else {
174+
if device.Push != current.Push {
175+
delete(push.PushDevices, current.Id)
176+
if device.Push != "" {
177+
push.PushDevices[device.Id] = device.Push
178+
}
179+
}
180+
current.Push = device.Push
181+
}
182+
164183
current.Enable = device.Enable
165184
current.Logging = device.Logging
166185
current.Tags = device.Tags

0 commit comments

Comments
 (0)