• Stars
    star
    763
  • Rank 59,064 (Top 2 %)
  • Language
    Swift
  • License
    BSD 2-Clause "Sim...
  • Created over 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swift implementation of JSON Web Token (JWT).

JSON Web Token

Build Status

Swift implementation of JSON Web Token.

Installation

Swift Pacakage Manager is the recommended installation method for JSONWebToken, CocoaPods is also supported.

pod 'JSONWebToken'

NOTE: Carthage may be supported, however support will not be provided for this installation method, use at your own risk if you know how it works.

Usage

import JWT

Encoding a claim

JWT.encode(claims: ["my": "payload"], algorithm: .hs256("secret".data(using: .utf8)!))

Encoding a claim set

var claims = ClaimSet()
claims.issuer = "fuller.li"
claims.issuedAt = Date()
claims["custom"] = "Hi"

JWT.encode(claims: claims, algorithm: .hs256("secret".data(using: .utf8)!))

Building a JWT with the builder pattern

JWT.encode(.hs256("secret".data(using: .utf8))) { builder in
  builder.issuer = "fuller.li"
  builder.issuedAt = Date()
  builder["custom"] = "Hi"
}

Decoding a JWT

When decoding a JWT, you must supply one or more algorithms and keys.

do {
  let claims: ClaimSet = try JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w", algorithm: .hs256("secret".data(using: .utf8)!))
  print(claims)
} catch {
  print("Failed to decode JWT: \(error)")
}

When the JWT may be signed with one out of many algorithms or keys:

try JWT.decode("eyJh...5w", algorithms: [
  .hs256("secret".data(using: .utf8)!),
  .hs256("secret2".data(using: .utf8)!),
  .hs512("secure".data(using: .utf8)!)
])

You might also want to give your iat, exp and nbf checks some kind of leeway to account for skewed clocks. You can do this by passing a leeway parameter like this:

try JWT.decode("eyJh...5w", algorithm: .hs256("secret".data(using: .utf8)!), leeway: 10)

Supported claims

The library supports validating the following claims:

  • Issuer (iss) Claim
  • Expiration Time (exp) Claim
  • Not Before (nbf) Claim
  • Issued At (iat) Claim
  • Audience (aud) Claim

Algorithms

This library supports the following algorithms:

  • none - Unsecured JWTs
  • hs256 - HMAC using SHA-256 hash algorithm (default)
  • hs384 - HMAC using SHA-384 hash algorithm
  • hs512 - HMAC using SHA-512 hash algorithm

License

JSONWebToken is licensed under the BSD license. See LICENSE for more info.

More Repositories

1

swiftenv

Swift Version Manager
Shell
1,973
star
2

Commander

Compose beautiful command line interfaces in Swift
Swift
1,530
star
3

Mockingjay

An elegant library for stubbing HTTP requests with ease in Swift
Swift
1,490
star
4

PathKit

Effortless path operations in Swift
Swift
1,453
star
5

Spectre

BDD Framework and test runner for Swift projects and playgrounds
Swift
403
star
6

JSONSchema.swift

JSON Schema validator in Swift
Swift
274
star
7

URITemplate.swift

Swift implementation of URI Template (RFC6570)
Swift
199
star
8

apiblueprint.vim

This vim plugin brings syntax highlighting and linting for API Blueprint.
Vim Script
113
star
9

dotfiles

kylef's dotfile
Vim Script
33
star
10

swiftenv-api

API for swiftenv to return the available versions of Swift
Python
25
star
11

draughtsman

API Blueprint Parser for Python 3
Python
23
star
12

changelog

A formal specification for changelog files and entries
18
star
13

goji

Command line JIRA client
Python
18
star
14

swiftenv-docker

This repository contains swiftenv images for Docker
Makefile
18
star
15

irctk

A Python IRC client library
Python
6
star
16

maintain

A unified interface to maintaining projects of any language.
Python
6
star
17

ucp

A simple and universal command line tool for capturing screenshots and uploading to the web.
Shell
5
star
18

redirector

Simple web server for HTTP redirecting from one domain to another.
Python
4
star
19

pygments-apiblueprint

An API Blueprint Lexer for Pygments
Python
3
star
20

refract.py

A Python library for interacting with Refract
Python
3
star
21

life

This repository is used as a personal issue tracker
2
star
22

homebrew-formulae

Ruby
1
star