<commit_before>import HTTP
import Vapor

let oauthHelperKey = "oauth-helper"

public final class Helper {

    public static func setup(for request: Request, tokenIntrospectionEndpoint: String, client: ClientFactoryProtocol,
                             resourceServerUsername: String, resourceServerPassword: String) {
        let helper = Helper(request: request, tokenIntrospectionEndpoint: tokenIntrospectionEndpoint, client: client,
                            resourceServerUsername: resourceServerUsername, resourceServerPassword: resourceServerPassword)
        request.storage[oauthHelperKey] = helper
    }

    let oauthHelper: OAuthHelper

    init(request: Request, provider: OAuth2Provider?) {
        self.oauthHelper = LocalOAuthHelper(request: request, tokenAuthenticator: provider?.tokenHandler.tokenAuthenticator,
                                            userManager: provider?.userManager, tokenManager: provider?.tokenManager)
    }

    init(request: Request, tokenIntrospectionEndpoint: String, client: ClientFactoryProtocol,
         resourceServerUsername: String, resourceServerPassword: String) {
        self.oauthHelper = RemoteOAuthHelper(request: request, tokenIntrospectionEndpoint: tokenIntrospectionEndpoint,
                                             client: client, resourceServerUsername: resourceServerUsername,
                                             resourceServerPassword: resourceServerPassword)
    }

    public func assertScopes(_ scopes: [String]?) throws {
        try oauthHelper.assertScopes(scopes)
    }

    public func user() throws -> OAuthUser {
        return try oauthHelper.user()
    }
}

extension Request {
    public var oauth: Helper {
        if let existing = storage[oauthHelperKey] as? Helper {
            return existing
        }

        let helper = Helper(request: self, provider: Request.oauthProvider)
        storage[oauthHelperKey] = helper

        return helper
    }

    static var oauthProvider: OAuth2Provider?
}

extension Request {
    func getOAuthToken() throws -> String {
        guard let authHeader = headers[.authorization] else {
            throw Abort(.forbidden)
        }

        guard authHeader.lowercased().hasPrefix("bearer ") else {
            throw Abort(.forbidden)
        }

        let token = authHeader.substring(from: authHeader.index(authHeader.startIndex, offsetBy: 7))

        guard !token.isEmpty else {
            throw Abort(.forbidden)
        }

        return token
    }
}<commit_msg>Remove warning from Swift 4<commit_after>import HTTP
import Vapor

let oauthHelperKey = "oauth-helper"

public final class Helper {

    public static func setup(for request: Request, tokenIntrospectionEndpoint: String, client: ClientFactoryProtocol,
                             resourceServerUsername: String, resourceServerPassword: String) {
        let helper = Helper(request: request, tokenIntrospectionEndpoint: tokenIntrospectionEndpoint, client: client,
                            resourceServerUsername: resourceServerUsername, resourceServerPassword: resourceServerPassword)
        request.storage[oauthHelperKey] = helper
    }

    let oauthHelper: OAuthHelper

    init(request: Request, provider: OAuth2Provider?) {
        self.oauthHelper = LocalOAuthHelper(request: request, tokenAuthenticator: provider?.tokenHandler.tokenAuthenticator,
                                            userManager: provider?.userManager, tokenManager: provider?.tokenManager)
    }

    init(request: Request, tokenIntrospectionEndpoint: String, client: ClientFactoryProtocol,
         resourceServerUsername: String, resourceServerPassword: String) {
        self.oauthHelper = RemoteOAuthHelper(request: request, tokenIntrospectionEndpoint: tokenIntrospectionEndpoint,
                                             client: client, resourceServerUsername: resourceServerUsername,
                                             resourceServerPassword: resourceServerPassword)
    }

    public func assertScopes(_ scopes: [String]?) throws {
        try oauthHelper.assertScopes(scopes)
    }

    public func user() throws -> OAuthUser {
        return try oauthHelper.user()
    }
}

extension Request {
    public var oauth: Helper {
        if let existing = storage[oauthHelperKey] as? Helper {
            return existing
        }

        let helper = Helper(request: self, provider: Request.oauthProvider)
        storage[oauthHelperKey] = helper

        return helper
    }

    static var oauthProvider: OAuth2Provider?
}

extension Request {
    func getOAuthToken() throws -> String {
        guard let authHeader = headers[.authorization] else {
            throw Abort(.forbidden)
        }

        guard authHeader.lowercased().hasPrefix("bearer ") else {
            throw Abort(.forbidden)
        }

        #if swift(>=4)
        let token  = String(authHeader[authHeader.index(authHeader.startIndex, offsetBy: 7)...])
        #else
        let token = authHeader.substring(from: authHeader.index(authHeader.startIndex, offsetBy: 7))
        #endif

        guard !token.isEmpty else {
            throw Abort(.forbidden)
        }

        return token
    }
}
{'fraction_non_alphanumeric': 0.07405270244277745, 'fraction_numerical': 0.0017311021350259665, 'mean_word_length': 2.6036036036036037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '351778', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1246, 'n_words': 315}