AWS SDK for Go (PILOT)
API Reference

PREVIEW DOCUMENTATION - This is a preview of a new format for the AWS SDK for Go API Reference documentation. For the current AWS SDK for Go API Reference, see https://docs.aws.amazon.com/sdk-for-go/api/.

We welcome your feedback on this new version of the documentation. Send your comments to aws-sdkdocs-feedback@amazon.com.

CookieSigner

import "github.com/aws/aws-sdk-go/service/cloudfront/sign"

type CookieSigner struct { Opts CookieOptions }

A CookieSigner provides signing utilities to sign Cookies for Amazon CloudFront resources. Using a private key and Credential Key Pair key ID the CookieSigner only needs to be created once per Credential Key Pair key ID and private key.

More information about signed Cookies and their structure can be found at: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-setting-signed-cookie-custom-policy.html

To sign a Cookie, create a CookieSigner with your private key and credential pair key ID. Once you have a CookieSigner instance you can call Sign or SignWithPolicy to sign the URLs.

The signer is safe to use concurrently, but the optional cookies options are not safe to modify concurrently.

Opts

A CookieOptions optional additional options that can be applied to the signed cookies.

Method

Sign

func (s CookieSigner) Sign(u string, expires time.Time, opts ...func(*CookieOptions)) ([]*http.Cookie, error)

Sign returns the cookies needed to allow user agents to make arbetrary requests to cloudfront for the resource(s) defined by the policy.

Sign will create a CloudFront policy with only a resource and condition of DateLessThan equal to the expires time provided.

The returned slice cookies should all be added to the Client's cookies or server's response.

Example:

s := sign.NewCookieSigner(keyID, privKey) // Get Signed cookies for a resource that will expire in 1 hour cookies, err := s.Sign("*", time.Now().Add(1 * time.Hour)) if err != nil { fmt.Println("failed to create signed cookies", err) return } // Or get Signed cookies for a resource that will expire in 1 hour // and set path and domain of cookies cookies, err := s.Sign("*", time.Now().Add(1 * time.Hour), func(o *sign.CookieOptions) { o.Path = "/" o.Domain = ".example.com" }) if err != nil { fmt.Println("failed to create signed cookies", err) return } // Server Response via http.ResponseWriter for _, c := range cookies { http.SetCookie(w, c) } // Client request via the cookie jar if client.CookieJar != nil { for _, c := range cookies { client.Cookie(w, c) } }

Example

CookieSigner Sign
{ privKey := mock.RSAPrivateKey cookieSigner := NewCookieSigner("keyID", privKey) cookies, err := cookieSigner.Sign("http://example.com/somepath/*", testSignTime.Add(30*time.Minute)) if err != nil { fmt.Println("failed to sign cookies with policy,", err) return } printExampleCookies(cookies) }

SignWithPolicy

func (s CookieSigner) SignWithPolicy(p *Policy, opts ...func(*CookieOptions)) ([]*http.Cookie, error)

SignWithPolicy returns the cookies needed to allow user agents to make arbetrairy requets to cloudfront for the resource(s) defined by the policy.

The returned slice cookies should all be added to the Client's cookies or server's response.

Example:

s := sign.NewCookieSigner(keyID, privKey) policy := &sign.Policy{ Statements: []sign.Statement{ { // Read the provided documentation on how to set this // correctly, you'll probably want to use wildcards. Resource: rawCloudFrontURL, Condition: sign.Condition{ // Optional IP source address range IPAddress: &sign.IPAddress{SourceIP: "192.0.2.0/24"}, // Optional date URL is not valid until DateGreaterThan: &sign.AWSEpochTime{time.Now().Add(30 * time.Minute)}, // Required date the URL will expire after DateLessThan: &sign.AWSEpochTime{time.Now().Add(1 * time.Hour)}, }, }, }, } // Get Signed cookies for a resource that will expire in 1 hour cookies, err := s.SignWithPolicy(policy) if err != nil { fmt.Println("failed to create signed cookies", err) return } // Or get Signed cookies for a resource that will expire in 1 hour // and set path and domain of cookies cookies, err := s.Sign(policy, func(o *sign.CookieOptions) { o.Path = "/" o.Domain = ".example.com" }) if err != nil { fmt.Println("failed to create signed cookies", err) return } // Server Response via http.ResponseWriter for _, c := range cookies { http.SetCookie(w, c) } // Client request via the cookie jar if client.CookieJar != nil { for _, c := range cookies { client.Cookie(w, c) } }

Example

CookieSigner SignWithPolicy
{ p := &Policy{ Statements: []Statement{{ Resource: "http://sub.cloudfront.com", Condition: Condition{ IPAddress: &IPAddress{SourceIP: "192.0.2.0/24"}, DateGreaterThan: &AWSEpochTime{testSignTime.Add(30 * time.Minute)}, DateLessThan: &AWSEpochTime{testSignTime.Add(1 * time.Hour)}, }, }, }, } privKey := mock.RSAPrivateKey keyID := "privateKeyID" cookieSigner := NewCookieSigner(keyID, privKey) cookies, err := cookieSigner.SignWithPolicy(p) if err != nil { fmt.Println("failed to sign cookies with policy,", err) return } printExampleCookies(cookies) }

On this page: