Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# Xcode
#
build/
*.pbxuser
!default.pbxuser

# Exclude the build directory
build/*

# Exclude temp nibs and swap files
*~.nib
*.swp

# Exclude OS X folder attributes
.DS_Store

# Exclude user-specific XCode 3 and 4 files
*.mode1
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspective
*.perspectivev3
!default.perspectivev3
*.pbxuser
*.xcworkspace
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate


# CocoaPods
#
Expand Down
3 changes: 0 additions & 3 deletions JWT workspace.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "D13A0161F3961F3380472D7F6128C9B2159DADA6",
"DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : {

},
"DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : {
"D13A0161F3961F3380472D7F6128C9B2159DADA6" : 0,
"CD87797B9D09AEEE7FEB1D43B38DA32D51BC7F68" : 0
},
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "FC5C1AE1-1DF3-43CF-89D3-D0C72142FD62",
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
"D13A0161F3961F3380472D7F6128C9B2159DADA6" : "swift-jwt\/",
"CD87797B9D09AEEE7FEB1D43B38DA32D51BC7F68" : "swift-sodium\/"
},
"DVTSourceControlWorkspaceBlueprintNameKey" : "JWT workspace",
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
"DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "JWT workspace.xcworkspace",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [
{
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "github.com:ziogaschr\/swift-sodium.git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "CD87797B9D09AEEE7FEB1D43B38DA32D51BC7F68"
},
{
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "github.com:stannie\/swift-jwt.git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "D13A0161F3961F3380472D7F6128C9B2159DADA6"
}
]
}
82 changes: 67 additions & 15 deletions JWT-Playground.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,79 @@
// Playground - noun: a place where people can play

/*:
### Table of Contents

1. [HS256 Load](HS256 Load)
2. [HS256 Dump](HS256 Dump)
3. [Ed25519 Dump](Ed25519 Dump)
*/

import SwiftJWT

// Example: load a HS256 signed JWT from a sting, and generate a new one with a new password
/*:
### HS256 Load
Example: load a HS256 signed JWT from a string, and generate a new one with a new password
*/

let jwt_str = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6InBob25lIiwicGhvbmVfbnVtYmVyIjoiKzE2NTAyODU1NjAwIiwidHlwZSI6ImxhbmRsaW5lIiwiYXVkIjoiaHR0cHM6Ly9hdXRoZW50aXEuY29tIn0.CAqNpbmOA9lz9aq7Sp1NqqbdLJARmFKY3L7CKcgXLNU"

var jwt = JWT(algorithms: ["none","HS256"])
var j = jwt.loads(jwt_str, key: "secret")
println(jwt.dumps("geheim"))
var jwt = JWT(algorithms: ["none", "HS256"])

do {
try jwt.loads(jwt_str, key: "secret")
print(jwt.header)
print(jwt.body)

let new_jwt = try jwt.dumps("geheim")

assert(jwt_str != new_jwt)

} catch {
print(error)
print("Validate of SignedJWT failed")
}


/*:
### HS256 Dump
Example: dump a HS256 signed JWT from a dictionary
*/

jwt = JWT(header: ["alg":"HS256"],
body: [
"sub": "1234567890",
"name": "John Doe",
"admin": true
], algorithms: nil)

do {
print(try jwt.dumps("secret"))

} catch {
print(error)
print("Validate of SignedJWT failed")
}


/*:
### Ed25519 Load
Example: load an Ed25519 signed JWT from a string
*/

let jwt_ed = "eyJhbGciOiJFZDI1NTE5IiwidHlwIjoiSldUIiwia2lkIjoiWE43VnBFWDF1Q3h4aHZ3VXVhY1lodVU5dDZ1eGdMYWhSaUxlU0VIRU5payJ9.eyJmb28iOiJiYXIifQ.a2dDcKXByKxiouOLnXUm7YUKHMGOU3yn_g91C90e8YmKjlF1_9ylAKukfMm6Y6WS3dZp2ysaglzzTnVxnRYyDQ"
let sk = "YHWUUc0P6SY46WaDdnssE8NpFsQQxJrvmdOrpU9X0wU"
let pk = "XN7VpEX1uCxxhvwUuacYhuU9t6uxgLahRiLeSEHENik"
jwt = JWTNaCl(algorithms: ["Ed25519","none"])
jwt.loads(jwt_ed)
jwt.header["alg"] = "none"
println(jwt.dumps()!)

jwt = JWT(header: ["alg":"HS256"],
body: [
"sub": "1234567890",
"name": "John Doe",
"admin": true
], algorithms: nil)
println(jwt.dumps("secret")!)

jwt = JWTNaCl(algorithms: ["Ed25519"])

do {
try jwt.loads(jwt_ed, verify: false, mandatory: ["foo"])

print(jwt.header)
print(jwt.body)

} catch {
print(error)
print("Validate of SignedJWT failed")
}
19 changes: 0 additions & 19 deletions JWT/CommonCrypto/CommonCrypto.h

This file was deleted.

15 changes: 0 additions & 15 deletions JWT/CommonCrypto/CommonCrypto.xcconfig

This file was deleted.

26 changes: 0 additions & 26 deletions JWT/CommonCrypto/Info.plist

This file was deleted.

4 changes: 0 additions & 4 deletions JWT/CommonCrypto/iphoneos.modulemap

This file was deleted.

4 changes: 0 additions & 4 deletions JWT/CommonCrypto/iphonesimulator.modulemap

This file was deleted.

4 changes: 0 additions & 4 deletions JWT/CommonCrypto/macosx.modulemap

This file was deleted.

36 changes: 0 additions & 36 deletions JWT/CommonCryptoTests/CommonCryptoTests.swift

This file was deleted.

24 changes: 0 additions & 24 deletions JWT/CommonCryptoTests/Info.plist

This file was deleted.

24 changes: 24 additions & 0 deletions JWT/JWT/Extensions/Data+JWT.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// NSData+JWT.swift
// SwiftJWT
//
// Created by Chris Ziogas on 05/11/15.
// Copyright © 2015 RoundZero bv. All rights reserved.
//

import Foundation

extension Data {
// MARK: - base64 extensions
func base64SafeUrlEncode(_ options: Data.Base64EncodingOptions = []) -> String {
// regular base64 encoding
var s = self.base64EncodedString(options: options)

// s = s.substringToIndex(s.endIndex.predecessor()) // remove last char
s = s.replacingOccurrences(of: "=", with: "") // Remove any trailing '='s
s = s.replacingOccurrences(of: "+", with: "-") // 62nd char of encoding
s = s.replacingOccurrences(of: "/", with: "_") // 63rd char of encoding

return s
}
}
29 changes: 29 additions & 0 deletions JWT/JWT/Extensions/String+JWT.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// String+JWT.swift
// SwiftJWT
//
// Created by Chris Ziogas on 05/11/15.
// Copyright © 2015 RoundZero bv. All rights reserved.
//

import Foundation

extension String {

// MARK: - base64 extensions
func base64SafeUrlDecode(_ options: Data.Base64DecodingOptions = []) -> Data? {
var s = self

s = s.replacingOccurrences(of: "-", with: "+") // 62nd char of encoding
s = s.replacingOccurrences(of: "_", with: "/") // 63rd char of encoding

switch (s.characters.count % 4) { // Pad with trailing '='s
case 0: break; // No pad chars in this case
case 2: s += "=="; break; // Two pad chars
case 3: s += "="; break; // One pad char
default: print("Illegal base64url string!")
}

return Data(base64Encoded: s, options: options)
}
}
2 changes: 1 addition & 1 deletion JWT/JWT/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>nl.roundzero.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Loading