func updateGoogleGet(c *gin.Context) { db := c.MustGet("db").(*database.Database) params := utils.ParseParams(c.Request) usr := params.GetByName("user") license := params.GetByName("license") valid, err := user.CheckLicense(db, license) if err != nil { switch err.(type) { case *database.NotFoundError: c.AbortWithError(404, err) default: c.AbortWithError(500, err) } return } if !valid { c.AbortWithError(401, err) return } err = google.Update(db, usr) if err != nil { c.AbortWithError(500, err) return } c.String(200, "") }
func callbackGoogleGet(c *gin.Context) { db := c.MustGet("db").(*database.Database) params := utils.ParseParams(c.Request) state := params.GetByName("state") code := params.GetByName("code") authErr := params.GetByName("error") switch authErr { case "": if state == "" || code == "" { c.AbortWithStatus(400) return } case "access_denied": // TODO Redirect to base callback url c.Redirect(301, "https://pritunl.com/") return default: c.AbortWithStatus(400) return } acct, tokn, err := google.Authorize(db, state, code) if err != nil { c.AbortWithError(500, err) return } if tokn.Version == 1 { query := fmt.Sprintf("state=%s&username=%s", tokn.RemoteState, url.QueryEscape(acct.Id)) hashFunc := hmac.New(sha512.New, []byte(tokn.RemoteSecret)) hashFunc.Write([]byte(query)) rawSignature := hashFunc.Sum(nil) sig := base64.URLEncoding.EncodeToString(rawSignature) url := fmt.Sprintf("%s?%s&sig=%s", tokn.RemoteCallback, query, url.QueryEscape(sig)) c.Redirect(301, url) } else { hashFunc := hmac.New(sha256.New, []byte(tokn.RemoteSecret)) hashFunc.Write([]byte(tokn.RemoteState + acct.Id)) rawSignature := hashFunc.Sum(nil) sig := base64.URLEncoding.EncodeToString(rawSignature) c.Redirect(301, fmt.Sprintf("%s?state=%s&user=%s&sig=%s", tokn.RemoteCallback, tokn.RemoteState, url.QueryEscape(acct.Id), sig)) } }