<commit_before>//
//  MultiAuthService.swift
//  MultiAuth
//
//  Created by <NAME> on 2/10/16.
//  Copyright © 2016 BSN Design. All rights reserved.
//

import UIKit
import MobileCoreServices

struct AuthenticationService {
    
    // MARK: - Public properties
    
    var networkAccess: AuthenticationNetworkAccess = AuthenticationNetworkAPIAccess()
    var handler: ((username: String?, password: String?, errorMessage: String?) -> ())?
    
    
    // MARK: - Public constants
    
    static let findLoginAction = "org.appextension.find-login-action"
    static let URLStringKey = "url_string"
    
    
    // MARK: - Constants
    
    private static let usernameKey = "username"
    private static let passwordKey = "password"
    private static let logInViaSharedCredentialsKey = "logInViaSharedCredentials"
    
    
    // MARK: - Private properties
    
    private let serverPath: String
    
    
    // MARK: - Initializer
    
    init(serverPath: String) {
        self.serverPath = serverPath
    }
    
    
    // MARK: - Public API
    
    func logIn(username: String, password: String, completion: (error: String?) -> Void) {
        networkAccess.logIn(username, password: password) { success, error in
            if success && error == nil {
                self.saveSharedCredentials(username: username, password: password)
                completion(error: nil)
            } else if let error = error as? NSError {
                completion(error: error.localizedDescription)
            } else {
                completion(error: "Unknown error")
            }
        }
    }
    
    func logOut() {
        networkAccess.logOut()
    }
    
    func retrieveCredentials(URLString: String, viewController: UIViewController, sender: AnyObject) {
        let activityViewController = configuredActivityViewController(URLString, sender: sender)
        activityViewController.completionWithItemsHandler = { activityType, completed, returnedItems, activityError in<commit_msg>Add protocol for network access<commit_after>//
//  MultiAuthService.swift
//  MultiAuth
//
//  Created by <NAME> on 2/10/16.
//  Copyright © 2016 BSN Design. All rights reserved.
//

import UIKit
import MobileCoreServices

// MARK: - MultiAuth Network Access Protocol

protocol MultiAuthNetworkAccess {
    func logIn(username: String, password: String, completion: (success: Bool, error: ErrorType?) -> Void)
    func logOut()
}

struct AuthenticationService {
    
    // MARK: - Public properties
    
    var handler: ((username: String?, password: String?, errorMessage: String?) -> ())?
    
    
    // MARK: - Public constants
    
    static let findLoginAction = "org.appextension.find-login-action"
    static let URLStringKey = "url_string"
    
    
    // MARK: - Constants
    
    private static let usernameKey = "username"
    private static let passwordKey = "password"
    private static let logInViaSharedCredentialsKey = "logInViaSharedCredentials"
    
    
    // MARK: - Private properties
    
    private let serverPath: String
    private let networkAccess: MultiAuthNetworkAccess
    
    
    // MARK: - Initializer
    
    init(serverPath: String, networkAccess: MultiAuthNetworkAccess) {
        self.serverPath = serverPath
        self.networkAccess = networkAccess
    }
    
    
    // MARK: - Public API
    
    func logIn(username: String, password: String, completion: (error: String?) -> Void) {
        networkAccess.logIn(username, password: password) { success, error in
            if success && error == nil {
                self.saveSharedCredentials(username: username, password: password)
                completion(error: nil)
            } else if let error = error as? NSError {
                completion(error: error.localizedDescription)
            } else {
                completion(error: "Unknown error")
            }
        }
    }
    
    func logOut() {
        networkAccess.logOut()
    }
    
    func retrieveCredentials(URLString: String, viewController: UIViewController, sender: AnyObject) {
        let activityViewController = configuredActivityViewController(URLString, sender: sender)
        activityViewController.completionWithItemsHandler = { activityType, completed, returnedItems, activityError in
{'fraction_non_alphanumeric': 0.08900279589934762, 'fraction_numerical': 0.004193849021435228, 'mean_word_length': 2.504489795918367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '223333', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1104, 'n_words': 307}