github.com/louisevanderlith/droxolite@v1.20.2/open/protector.go (about)

     1  package open
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/base64"
     6  	"net/http"
     7  )
     8  
     9  type Protector interface {
    10  }
    11  
    12  func RedirectToLastLocation(w http.ResponseWriter, r *http.Request) {
    13  	location, err := r.Cookie("location")
    14  
    15  	if err != nil {
    16  		http.Redirect(w, r, "/", http.StatusFound)
    17  		return
    18  	}
    19  
    20  	http.Redirect(w, r, location.Value, http.StatusFound)
    21  }
    22  
    23  func generateStateOauthCookie(w http.ResponseWriter) string {
    24  	b := make([]byte, 16)
    25  	rand.Read(b)
    26  	state := base64.URLEncoding.EncodeToString(b)
    27  	cookie := http.Cookie{
    28  		Name:     "oauthstate",
    29  		Value:    state,
    30  		MaxAge:   0,
    31  		Path:     "/",
    32  		HttpOnly: true,
    33  	}
    34  	http.SetCookie(w, &cookie)
    35  
    36  	return state
    37  }
    38  
    39  func setLastLocationCookie(w http.ResponseWriter, url string) {
    40  	http.SetCookie(w, &http.Cookie{
    41  		Name:     "location",
    42  		Value:    url,
    43  		MaxAge:   0,
    44  		Secure:   false,
    45  		HttpOnly: true,
    46  		Path:     "/",
    47  	})
    48  }