This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f73ed57
commit 06cf234
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
GPL Session | ||
============== | ||
|
||
Manage sessions in your Solana Anchor Programs. | ||
|
||
|
||
# Installation | ||
|
||
```bash | ||
cargo add gpl-session --features no-entrypoint | ||
``` | ||
|
||
# Usage | ||
|
||
1. Import the dependencies | ||
|
||
```rust | ||
use gpl_session::{SessionError, SessionToken, session_auth_or, Session}; | ||
``` | ||
|
||
2. Derive the `Session` trait on your instruction struct | ||
|
||
```rust | ||
#[derive(Accounts, Session)] | ||
pub struct Instruction<'info> { | ||
..... | ||
pub user: Account<'info, User>, | ||
|
||
#[session( | ||
signer = authority, | ||
authority = user.authority.key() | ||
)] | ||
pub session_token: Option<Account<'info, SessionToken>>, | ||
|
||
#[account(mut)] | ||
pub authority: Signer<'info>, | ||
..... | ||
} | ||
``` | ||
|
||
3. Add the `session_auth_or` macro to your instruction handler with fallback logic on who the instruction should validate the signer when sessions are not present and an appropirate ErrorCode. If you've used `require*!` macros in anchor_lang you already know how this works. | ||
|
||
```rust | ||
#[session_auth_or( | ||
ctx.accounts.user.authority.key() == ctx.accounts.authority.key(), | ||
ErrorCode | ||
)] | ||
pub fn ix_handler(ctx: Context<Instruction>,) -> Result<()> { | ||
..... | ||
} | ||
|
||
``` |