update recieve link cb
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/sentriz/gonic/db"
|
||||
"github.com/sentriz/gonic/handler/utilities"
|
||||
"github.com/sentriz/gonic/lastfm"
|
||||
)
|
||||
|
||||
func (c *Controller) ServeLogin(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -83,7 +84,23 @@ func (c *Controller) ServeLinkLastFMCallback(w http.ResponseWriter, r *http.Requ
|
||||
http.Error(w, "please provide a token", 400)
|
||||
return
|
||||
}
|
||||
_ = token
|
||||
var apiKey db.Setting
|
||||
c.DB.Where("key = ?", "lastfm_api_key").First(&apiKey)
|
||||
var secret db.Setting
|
||||
c.DB.Where("key = ?", "lastfm_secret").First(&secret)
|
||||
sessionKey, err := lastfm.GetSession(
|
||||
apiKey.Value,
|
||||
secret.Value,
|
||||
token,
|
||||
)
|
||||
if err != nil {
|
||||
session := r.Context().Value("session").(*sessions.Session)
|
||||
session.AddFlash(err.Error())
|
||||
session.Save(r, w)
|
||||
http.Redirect(w, r, "/admin/home", 302)
|
||||
return
|
||||
}
|
||||
fmt.Println("THE SESSION KEY", sessionKey)
|
||||
}
|
||||
|
||||
func (c *Controller) ServeChangePassword(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -164,10 +181,10 @@ func (c *Controller) ServeCreateUserDo(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *Controller) ServeUpdateLastFMAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
var data templateData
|
||||
var apiKey db.Setting
|
||||
var secret db.Setting
|
||||
c.DB.Where("key = ?", "lastfm_api_key").First(&apiKey)
|
||||
c.DB.Where("key = ?", "lastfm_secret").First(&secret)
|
||||
data.CurrentLastFMAPIKey = apiKey.Value
|
||||
var secret db.Setting
|
||||
c.DB.Where("key = ?", "lastfm_secret").First(&secret)
|
||||
data.CurrentLastFMAPISecret = secret.Value
|
||||
renderTemplate(w, r, "update_lastfm_api_key", &data)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/sentriz/gonic/db"
|
||||
"github.com/sentriz/gonic/handler/utilities"
|
||||
"github.com/sentriz/gonic/subsonic"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
@@ -142,21 +143,6 @@ func respondError(w http.ResponseWriter, r *http.Request,
|
||||
))
|
||||
}
|
||||
|
||||
func firstExisting(or string, strings ...string) string {
|
||||
current := ""
|
||||
for _, s := range strings {
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
current = s
|
||||
break
|
||||
}
|
||||
if current == "" {
|
||||
return or
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
func renderTemplate(w http.ResponseWriter, r *http.Request,
|
||||
name string, data *templateData) {
|
||||
session := r.Context().Value("session").(*sessions.Session)
|
||||
@@ -169,13 +155,13 @@ func renderTemplate(w http.ResponseWriter, r *http.Request,
|
||||
if ok {
|
||||
data.User = user
|
||||
}
|
||||
scheme := firstExisting(
|
||||
scheme := utilities.FirstExisting(
|
||||
"http", // fallback
|
||||
r.Header.Get("X-Forwarded-Proto"),
|
||||
r.Header.Get("X-Forwarded-Scheme"),
|
||||
r.URL.Scheme,
|
||||
)
|
||||
host := firstExisting(
|
||||
host := utilities.FirstExisting(
|
||||
"localhost:7373", // fallback
|
||||
r.Header.Get("X-Forwarded-Host"),
|
||||
r.Host,
|
||||
|
||||
@@ -25,3 +25,18 @@ func ValidateAPIKey(apiKey, secret string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FirstExisting(or string, strings ...string) string {
|
||||
current := ""
|
||||
for _, s := range strings {
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
current = s
|
||||
break
|
||||
}
|
||||
if current == "" {
|
||||
return or
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func getParamSignature(params url.Values, secret string) string {
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
func GetSession(apiKey, secret, token string) (error, string) {
|
||||
func GetSession(apiKey, secret, token string) (string, error) {
|
||||
params := url.Values{}
|
||||
// the first 3 parameters here must be in alphabetical order
|
||||
params.Add("api_key", apiKey)
|
||||
@@ -39,17 +39,17 @@ func GetSession(apiKey, secret, token string) (error, string) {
|
||||
req.URL.RawQuery = params.Encode()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error when making request to last.fm: %v", err), ""
|
||||
return "", fmt.Errorf("error when making request to last.fm: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
decoder := xml.NewDecoder(resp.Body)
|
||||
var lastfm LastFM
|
||||
err = decoder.Decode(&lastfm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error when decoding last.fm response: %v", err), ""
|
||||
return "", fmt.Errorf("error when decoding last.fm response: %v", err)
|
||||
}
|
||||
if lastfm.Error != nil {
|
||||
return fmt.Errorf("error when parsing last.fm response: %v", lastfm.Error.Value), ""
|
||||
return "", fmt.Errorf("error when parsing last.fm response: %v", lastfm.Error.Value)
|
||||
}
|
||||
return nil, lastfm.Session.Key
|
||||
return lastfm.Session.Key, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user