Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Dec 1, 2023
1 parent 39527c3 commit c8a0010
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 2
updates:
- package-ecosystem: "gomod"
commit-message:
prefix: "deps:"
directory: "/"
schedule:
interval: "weekly"
day: "sunday"
time: "09:00"
- package-ecosystem: "github-actions"
commit-message:
prefix: "ci:"
directory: "/"
schedule:
interval: "weekly"
day: "sunday"
time: "09:00"
17 changes: 17 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: build

on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
- cron: '0 0 * * 0' # run "At 00:00 on Sunday"

# See https://github.com/cristalhq/.github/.github/workflows
jobs:
build:
uses: cristalhq/.github/.github/workflows/[email protected]

vuln:
uses: cristalhq/.github/.github/workflows/[email protected]
1 change: 1 addition & 0 deletions GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Guide for paseto
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
# paseto
# paseto

[![build-img]][build-url]
[![pkg-img]][pkg-url]
[![reportcard-img]][reportcard-url]
[![coverage-img]][coverage-url]
[![version-img]][version-url]

PASETO (Platform-Agnostic Security Tokens implementation) in Go [paseto](https://paseto.io).

See also JWT in Go [cristalhq/jwt](https://github.com/cristalhq/jwt).

## Features

* Simple API.
* Clean and tested code.
* Optimized for speed.
* Concurrent-safe.

See [docs][pkg-url] or [GUIDE.md](https://github.com/cristalhq/paseto/blob/main/GUIDE.md) for more details.

## Install

Go version 1.18+

```
go get github.com/cristalhq/paseto
```

## Example

```go
// TODO
```

See examples: [example_test.go](https://github.com/cristalhq/paseto/blob/main/example_test.go).

## License

[MIT License](LICENSE).

[build-img]: https://github.com/cristalhq/paseto/workflows/build/badge.svg
[build-url]: https://github.com/cristalhq/paseto/actions
[pkg-img]: https://pkg.go.dev/badge/cristalhq/paseto
[pkg-url]: https://pkg.go.dev/github.com/cristalhq/paseto
[reportcard-img]: https://goreportcard.com/badge/cristalhq/paseto
[reportcard-url]: https://goreportcard.com/report/cristalhq/paseto
[coverage-img]: https://codecov.io/gh/cristalhq/paseto/branch/main/graph/badge.svg
[coverage-url]: https://codecov.io/gh/cristalhq/paseto
[version-img]: https://img.shields.io/github/v/release/cristalhq/paseto
[version-url]: https://github.com/cristalhq/paseto/releases
5 changes: 5 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package paseto_test

func Example() {
// Output:
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module paseto

go 1.18
83 changes: 83 additions & 0 deletions paseto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package paseto

import "encoding/json"

type Token struct {
typ TokenType
raw []byte
dot1 int
dot2 int
claims json.RawMessage
footer json.RawMessage
}

func (t Token) Type() TokenType {
return t.typ
}

func (t *Token) String() string {
return string(t.raw)
}

func (t *Token) Bytes() []byte {
return t.raw
}

// HeaderPart returns token header part.
func (t *Token) HeaderPart() []byte {
return t.raw[:t.dot1]
}

// ClaimsPart returns token claims part.
func (t *Token) ClaimsPart() []byte {
return t.raw[t.dot1+1 : t.dot2]
}

// PayloadPart returns token payload part.
func (t *Token) PayloadPart() []byte {
return t.raw[:t.dot2]
}

// SignaturePart returns token signature part.
func (t *Token) SignaturePart() []byte {
return t.raw[t.dot2+1:]
}

// Claims returns token's claims.
func (t *Token) Claims() json.RawMessage {
return t.claims
}

// DecodeClaims into a given parameter.
func (t *Token) DecodeClaims(dst any) error {
return json.Unmarshal(t.claims, dst)
}

// Footer returns token's footer.
func (t *Token) Footer() json.RawMessage {
return t.footer
}

// DecodeFooter into a given parameter.
func (t *Token) DecodeFooter(dst any) error {
return json.Unmarshal(t.footer, dst)
}

// unexported method to check that token was created via Parse func.
func (t *Token) isValid() bool {
return t != nil && len(t.raw) > 0
}

type TokenType uint

const (
TokenUnknown TokenType = 0
TokenV1Local TokenType = 1
TokenV1Public TokenType = 2
TokenV2Local TokenType = 3
TokenV2Public TokenType = 4
TokenV3Local TokenType = 5
TokenV3Public TokenType = 6
TokenV4Local TokenType = 7
TokenV4Public TokenType = 8
)

0 comments on commit c8a0010

Please sign in to comment.