Code Monkey home page Code Monkey logo

Comments (4)

youknow98 avatar youknow98 commented on May 14, 2024 1

Thank you very much!

from iris.

kataras avatar kataras commented on May 14, 2024

I tested the below, which is identical to your use case and works as expected, please make good post issues I don't have the necessary info to help you... try to find out what your rest application does and fails on you

	app := iris.New()

	cookieName := "mycustomsessionid"
	// AES only supports key sizes of 16, 24 or 32 bytes.
	// You either need to provide exactly that amount or you derive the key from what you type in.
	hashKey := []byte("the-big-and-secret-fash-key-here")
	blockKey := []byte("lot-secret-of-characters-big-too")
	secureCookie := securecookie.New(hashKey, blockKey)

	mySessions := sessions.New(sessions.Config{
		Cookie: cookieName,
		Encode: secureCookie.Encode,
		Decode: secureCookie.Decode,
                Expires: time.Hour * 1,
	})

	app.Post("/set_boolean", func(ctx context.Context) {
		s := mySessions.Start(ctx)
		s.Set("authenticated", true)
		s.Set("UserId", "42")
		ctx.Redirect("/get_boolean")
	})

	app.Get("/get_boolean", func(ctx context.Context) {
		s := mySessions.Start(ctx)
		auth, err := s.GetBoolean("authenticated")
		if !auth || err != nil {
			ctx.Writef("expected authenticated to be true!\n")
		}
		if userid := s.Get("UserId"); userid != "42" {
			ctx.Writef("expected UserId to be 42!")
		}
	})

from iris.

ZaniaDeveloper avatar ZaniaDeveloper commented on May 14, 2024

I found why it doesn't work, that's cause the browser cookie in response. The cookie is sent with empty value:

image

It should be like this:

image

from iris.

ZaniaDeveloper avatar ZaniaDeveloper commented on May 14, 2024

The problem is your hashKey, the length is too short, it must be 32 ou 68 octets. That's why, the cookie name is empty.

As it say in SecureCookie documentation:

// hashKey is required, used to authenticate values using HMAC. Create it using
// GenerateRandomKey(). It is recommended to use a key with 32 or 64 bytes.
//
// blockKey is optional, used to encrypt values. Create it using
// GenerateRandomKey(). The key length must correspond to the block size
// of the encryption algorithm. For AES, used by default, valid lengths are
// 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
// The default encoder used for cookie serialization is encoding/gob.

from iris.

Related Issues (20)

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.