-
Notifications
You must be signed in to change notification settings - Fork 1
Implements Sessions in the lock client #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dec93d2
eb89a51
635aa9f
53f0549
7571133
f3e07d7
229bca2
14f0216
55c9290
03ac63f
9f63823
de42353
61736c7
48ef72b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package lockclient | ||
|
|
||
| // Error provides constant error strings to the driver functions. | ||
| type Error string | ||
|
|
||
| func (e Error) Error() string { return string(e) } | ||
|
|
||
| // Constant errors. | ||
| // Rule of thumb, all errors start with a small letter and end with no full stop. | ||
| const ( | ||
| ErrSessionNonExistent = Error("the session related to this process doesn't exist") | ||
| ErrSessionExpired = Error("session expired") | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package id | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "math/rand" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/oklog/ulid" | ||
| ) | ||
|
|
||
| // ID describes a general identifier. An ID has to be unique application-wide. | ||
| // IDs must not be re-used. | ||
| type ID interface { | ||
| fmt.Stringer | ||
| Bytes() []byte | ||
| } | ||
|
|
||
| var _ ID = (*id)(nil) | ||
|
|
||
| type id ulid.ULID | ||
|
|
||
| var ( | ||
| lock sync.Mutex | ||
| randSource = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
| entropy = ulid.Monotonic(randSource, 0) | ||
| ) | ||
|
|
||
| // Create creates a globally unique ID. This function is safe for concurrent | ||
| // use. | ||
| func Create() ID { | ||
| lock.Lock() | ||
| defer lock.Unlock() | ||
|
|
||
| genID, err := ulid.New(ulid.Now(), entropy) | ||
| if err != nil { | ||
| // For this to happen, the random module would have to fail. Since we | ||
| // use Go's pseudo RNG, which just jumps around a few numbers, instead | ||
| // of using crypto/rand, and we also made this function safe for | ||
| // concurrent use, this is nearly impossible to happen. However, with | ||
| // the current version of oklog/ulid v1.3.1, this will also break after | ||
| // 2121-04-11 11:53:25.01172576 UTC. | ||
| log.Fatal(fmt.Errorf("new ulid: %w", err)) | ||
| } | ||
| return id(genID) | ||
| } | ||
|
|
||
| // Parse parses an ID from a byte slice. | ||
| func Parse(idBytes []byte) (ID, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't understand what this function does exactly ? Why are there two version of an ID - string and bytes? Also, it doesn't seem to be used elsewhere
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's recommended to pass the ID in type |
||
| parsed, err := ulid.Parse(string(idBytes)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parse: %w", err) | ||
| } | ||
| return id(parsed), nil | ||
| } | ||
|
|
||
| func (id id) String() string { | ||
| return ulid.ULID(id).String() | ||
| } | ||
|
|
||
| func (id id) Bytes() []byte { | ||
| return []byte(id.String()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package session | ||
|
|
||
| import "github.com/SystemBuilders/LocKey/internal/lockclient/id" | ||
|
|
||
| // Session captures all necessary parameters necessary to | ||
| // describe a session with the lockservice in the lockclient. | ||
| type Session interface { | ||
| // SessionID is the unique ID that represents this session. | ||
| // This will be used in every transaction for validating the user. | ||
| SessionID() id.ID | ||
| // ClientID is the ID of the client that will be created when | ||
| // the client is created. This acts as a second layer check along | ||
| // with the sessionID. | ||
| ClientID() id.ID | ||
| // ProcessID the unique ID assigned for the process by the client. | ||
| // This will be the third layer check in the security mechanism. | ||
| ProcessID() id.ID | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package session | ||
|
|
||
| import ( | ||
| "github.com/SystemBuilders/LocKey/internal/lockclient/id" | ||
| ) | ||
|
|
||
| var _ Session = (*SimpleSession)(nil) | ||
|
|
||
| // SimpleSession implements a session. | ||
| type SimpleSession struct { | ||
| sessionID id.ID | ||
| clientID id.ID | ||
| processID id.ID | ||
| } | ||
|
|
||
| // SessionID returns the sessionID of the SimpleSession. | ||
| func (s *SimpleSession) SessionID() id.ID { | ||
| return s.sessionID | ||
| } | ||
|
|
||
| // ClientID returns the clientID of the SimpleSession. | ||
| func (s *SimpleSession) ClientID() id.ID { | ||
| return s.clientID | ||
| } | ||
|
|
||
| // ProcessID returns the processID of the SimpleSession | ||
| func (s *SimpleSession) ProcessID() id.ID { | ||
| return s.processID | ||
| } | ||
|
|
||
| // NewSession returns a new instance of a session with the given parameters. | ||
| func NewSession(sessionID, clientID, processID id.ID) Session { | ||
| return &SimpleSession{ | ||
| sessionID: sessionID, | ||
| clientID: clientID, | ||
| processID: processID, | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.