Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from vapor/updates
Browse files Browse the repository at this point in the history
readme
  • Loading branch information
tanner0101 authored Mar 23, 2017
2 parents dd5e7b0 + aae47a6 commit 960db9e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 26 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# auth
# Auth

![Swift](http://img.shields.io/badge/swift-3.1-brightgreen.svg)
[![CircleCI](https://circleci.com/gh/vapor/auth.svg?style=shield)](https://circleci.com/gh/vapor/auth)
[![Slack Status](http://vapor.team/badge.svg)](http://vapor.team)

- [x] Token Authentication
- [x] Username/Password Authentication
- [x] Permission based Authorization

## 📖 Documentation

Visit the Vapor web framework's [documentation](http://docs.vapor.codes) for instructions on how to use this package.

## 💧 Community

Join the welcoming community of fellow Vapor developers in [slack](http://vapor.team).

## 🔧 Compatibility

This package has been tested on macOS and Ubuntu.
2 changes: 1 addition & 1 deletion Sources/Authentication/Header/Basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension AuthorizationHeader {

let token = string.substring(from: range.upperBound)

let decodedToken = token.makeBytes().base64Decoded.string
let decodedToken = token.makeBytes().base64Decoded.makeString()
guard let separatorRange = decodedToken.range(of: ":") else {
return nil
}
Expand Down
33 changes: 28 additions & 5 deletions Sources/Authorization/Authorizable.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Fluent

public protocol Authorizable: Entity {
public protocol Authorizable: Entity {}

}
// MARK: Pivot

extension Authorizable {
public func isAuthorized<
Expand Down Expand Up @@ -35,12 +35,35 @@ extension Authorizable {
PivotType.Left == Self,
PivotType.Right == PermissionType
{
guard try isAuthorized(to: permission, withPivot: PivotType.self) else {
throw AuthorizationError.notAuthorized
}
guard try isAuthorized(to: permission, withPivot: PivotType.self) else {
throw AuthorizationError.notAuthorized
}
}
}

extension PivotProtocol where Self.Right: Permission {
static func add(_ permissions: [Self.Right], to left: Self.Left) throws {
for permission in permissions {
try add(permission, to: left)
}
}

static func add(_ permission: Self.Right, to left: Self.Left) throws {
guard let permission = try Self.Right
.query()
.filter("key", permission.key)
.first()
else {
throw AuthorizationError.unknownPermission
}

try self.attach(left, permission)
}
}


// MARK: Double Pivot

extension Authorizable {
public func isAuthorized<
PermissionType: Permission,
Expand Down
26 changes: 8 additions & 18 deletions Tests/AuthenticationTests/Utilities/AuthUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,21 @@ import Node
import Fluent

final class AuthUser: Entity {
var id: Node?
var name: String
var exists: Bool = false
let storage = Storage()

init(name: String) {
self.name = name
}

init(node: Node, in context: Context) throws {
self.id = nil
self.name = try node.extract("name")
init(row: Row) throws {
name = try row.get("name")
}

func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"name": name
])
}

static func prepare(_ database: Database) throws {
}

static func revert(_ database: Database) throws {


func makeRow() throws -> Row {
var row = Row()
try row.set("name", name)
return row
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import XCTest
@testable import AuthenticationTests

XCTMain([
testCase(AuthenticationTests.allTests),
testCase(ExampleTests.allTests),
])
3 changes: 3 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
override:
- eval "$(curl -sL swift.vapor.sh/ci-3.1)"

0 comments on commit 960db9e

Please sign in to comment.