ausocean / cloud

GNU General Public License v3.0
1 stars 1 forks source link

TV: Add ytConfig to handle authentication of new channels to broadcast #161

Closed ao-david closed 5 days ago

ao-david commented 6 days ago

ytConfig stores oauth2 sessions in order to authenticate channels for broadcasts. This change is a precursor to preventing a pattern duplication for the callback handling.

saxon-milton commented 5 days ago

ytConfig stores oauth2 sessions in order to authenticate channels for broadcasts. This change is a precursor to preventing a pattern duplication for the callback handling.

I don't think this change is really a precursor for preventing pattern duplication; that can be done using a much simpler fix. Maybe we should implement the simple fix to resolve the pattern duplication issue, and spend more time thinking about this and how we can merge into the gauth package.

ao-david commented 5 days ago

that can be done using a much simpler fix.

Is the simple fix the boolean for the handler initialisation that you discussed? I think you could be right. If we are going to refactor soon anyway that might be a good quick fix to get by with.

saxon-milton commented 5 days ago

that can be done using a much simpler fix.

Is the simple fix the boolean for the handler initialisation that you discussed? I think you could be right. If we are going to refactor soon anyway that might be a good quick fix to get by with.

I think we can make it slightly less bad,

var authHandler *func(http.ResponseWriter, *http.Request)

func init() {
    http.HandleFunc(
        youtubeCredsRedirect,
        func(w http.ResponseWriter, r *http.Request) {
            (*authHandler)(w, r)
        },
    )
}

// genToken redirects the user to an authorisation page for generation of an
// authorisation token.
func genToken(w http.ResponseWriter, r *http.Request, config *oauth2.Config, url string) {
    config.RedirectURL = "https://" + r.Host + youtubeCredsRedirect

    handler := func(w http.ResponseWriter, r *http.Request) {
        code := r.FormValue("code")
        tok, err := config.Exchange(context.Background(), code)
        if err != nil {
            log.Printf("could not exchange token: %v", err)
        }

        if production {
            err = saveTokObj(context.Background(), tok, url)
        } else {
            err = saveTokFile(tok, url)
        }

        if err != nil {
            log.Printf("could not save new token: %v", err)
        }

        completionRedirect := "https://" + r.Host + "/admin/broadcast"
        http.Redirect(w, r, completionRedirect, http.StatusSeeOther)
    }
    authHandler = &handler

    authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
    http.Redirect(w, r, authURL, http.StatusSeeOther)
}
ao-david commented 5 days ago

that can be done using a much simpler fix.

Is the simple fix the boolean for the handler initialisation that you discussed? I think you could be right. If we are going to refactor soon anyway that might be a good quick fix to get by with.

I think we can make it slightly less bad,

var authHandler *func(http.ResponseWriter, *http.Request)

func init() {
  http.HandleFunc(
      youtubeCredsRedirect,
      func(w http.ResponseWriter, r *http.Request) {
          (*authHandler)(w, r)
      },
  )
}

// genToken redirects the user to an authorisation page for generation of an
// authorisation token.
func genToken(w http.ResponseWriter, r *http.Request, config *oauth2.Config, url string) {
  config.RedirectURL = "https://" + r.Host + youtubeCredsRedirect

  handler := func(w http.ResponseWriter, r *http.Request) {
      code := r.FormValue("code")
      tok, err := config.Exchange(context.Background(), code)
      if err != nil {
          log.Printf("could not exchange token: %v", err)
      }

      if production {
          err = saveTokObj(context.Background(), tok, url)
      } else {
          err = saveTokFile(tok, url)
      }

      if err != nil {
          log.Printf("could not save new token: %v", err)
      }

      completionRedirect := "https://" + r.Host + "/admin/broadcast"
      http.Redirect(w, r, completionRedirect, http.StatusSeeOther)
  }
  authHandler = &handler

  authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
  http.Redirect(w, r, authURL, http.StatusSeeOther)
}

See #164

ao-david commented 5 days ago

This change has been replaced by #164. A new implementation of a generalised gauth package will address many of these comments. (See #162)