GithubHelp home page GithubHelp logo

signed-url's Introduction

Create signed URLs

license

This package can create URLs by adding an expiration date and a signature to the URL. Signed URLs are especially useful for routes that are publicly accessible yet need a layer of protection against URL manipulation.

Installation

go get github.com/i-pva/signed-url

Example

package main

import (
    "log"
    "net/http"
    "net/url"
    
    surl "github.com/i-pva/signed-url"
)

func main(){

	surl.SetSecretKey([]byte("your-secret-key"))  //rewrite secret key


	u := &url.URL{
		Scheme: "https",
		Host:   "example.com",
		Path:   "path",
	}

	//URL Signing

	signedUrl , err := surl.Sign(u)
	if err != nil {
		log.Fatalln(err)
	}

	log.Println(signedUrl.String()) // https://example.com/path?signature=XXX
	

	// URL Validating

	if surl.HasValidURL(&http.Request{URL: u}) {
		log.Println("URL is valid")
		// do something
	}

}

URLs Signing with a limited lifetime

signedUrl , err := url.SignTemporary(u, 1 * time.Hour) // will be valid for 1 hour
if err != nil {
     return err
}

log.Println(signedUrl.String()) // https://example.com/path?expires=XXX&signature=XXX

URLs Validating with handler

mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("route under signed url"))
})
 
handler := surl.Handler(mux) // handler for validating signed URLs
http.ListenAndServe(":", handler)

This package is released under the MIT license.

signed-url's People

Contributors

i-pva avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.