<commit_before>//
//  RockAuthSession.swift
//  RockauthiOS
//
//  Created by <NAME> on 12/2/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

public typealias JWT = String

public class RockAuthSession {
    public let authentication: Authetication
    public let authentications: [Authetication]
    public let user: RockauthUser
    public let providerAuthentications: [ProviderAuthentication]
    
    init?(json: [String: AnyObject?]) {
        
        //local variables to pull out of the hash.  We use local variables so we can make a master nil check at the end so we can know if we should fail or not.
        //We fail in one place because of a compiler bug where we have to initialize all variables before returning nil.
        var localUser: RockauthUser?
        var localAuthentication: Authetication?
        var localAuthentications: [Authetication]?
        
        //User will either be in a collection or a top level hash.  This is done for consistencey but in practice there will only ever be one user
        if let usersArray = json["users"] as? [[String: AnyObject]], hash = usersArray.first, user = RockauthUser(json: hash) {
            localUser = user
        }
        else if let hash = json["user"] as? [String: AnyObject], user = RockauthUser(json: hash){
            localUser = user
        }
        
        //Same approach with authentication
        if let hash = json["authentication"] as? [String: AnyObject], let auth = Authetication(json: hash) {
            //In this case only 1 auth came down so that is the one we should use and the user object will NOT have a authentication_id key
            localAuthentications = [Authetication]()
            localAuthentications!.append(auth)
            localAuthentication = auth
        }
        else if let array = json["authentications"] as? [[String: AnyObject]] {
            localAuthentications = [Authetication]()
            for hash in array {
                if let auth = Authetication(json: hash) {
                    localAuthentications!.append(auth)
                }
            }
            
            //In this case the meta object should have the primaryResourceId key so we can look through all our auths and find the one we should be using for this session
            if let meta = json["meta"] as? [String: AnyObject], authID = meta["primaryResourceId"] as? Int{
                localAuthentication = localAuthentications!.filter{ $0.id == authID}.first
            }
        }
        
        //At this point we have collected all our values in the localy named ones so we can unwrap and assign them.  If they are non nil at this point we can fail
        if let u = localUser, let auth = localAuthentication, let auths = localAuthentications {
            self.user = u
            self.authentication = auth
            self.authentications = auths
        }
        else {
            self.user = RockauthUser()
            self.authentication = Authetication()
            self.authentications = [Authetication]()
            self.providerAuthentications = [ProviderAuthentication]()
            return nil
        }
        
        //pull the provider authentications out
        if let providerAuths = json["providerAuthentications"] as? [[String: AnyObject]] {
            var collector = [ProviderAuthentication]()
            for hash in providerAuths {
                if let auth = ProviderAuthentication(json: hash) {
                    collector.append(auth)
                }
            }
            self.providerAuthentications = collector
        }
        else{
            self.providerAuthentications = [ProviderAuthentication]()
        }
    }
}<commit_msg>Add the raw json to the rockauth session object so that if additional info is passed on login doesn't get lost if rockauth doesn't know how to parse it<commit_after>//
//  RockAuthSession.swift
//  RockauthiOS
//
//  Created by <NAME> on 12/2/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

public typealias JWT = String

public class RockAuthSession {
    public let authentication: Authetication
    public let authentications: [Authetication]
    public let user: RockauthUser
    public let providerAuthentications: [ProviderAuthentication]
    public let rawJSON: [String: AnyObject]
    
    init?(json: [String: AnyObject]) {
        
        //Storing the raw data that made up this object so that you have access to addition info which may be passed that doesn't get parsed by this class
        //or it's children
        self.rawJSON = json
        
        //local variables to pull out of the hash.  We use local variables so we can make a master nil check at the end so we can know if we should fail or not.
        //We fail in one place because of a compiler bug where we have to initialize all variables before returning nil.
        var localUser: RockauthUser?
        var localAuthentication: Authetication?
        var localAuthentications: [Authetication]?
        
        //User will either be in a collection or a top level hash.  This is done for consistencey but in practice there will only ever be one user
        if let usersArray = json["users"] as? [[String: AnyObject]], hash = usersArray.first, user = RockauthUser(json: hash) {
            localUser = user
        }
        else if let hash = json["user"] as? [String: AnyObject], user = RockauthUser(json: hash){
            localUser = user
        }
        
        //Same approach with authentication
        if let hash = json["authentication"] as? [String: AnyObject], let auth = Authetication(json: hash) {
            //In this case only 1 auth came down so that is the one we should use and the user object will NOT have a authentication_id key
            localAuthentications = [Authetication]()
            localAuthentications!.append(auth)
            localAuthentication = auth
        }
        else if let array = json["authentications"] as? [[String: AnyObject]] {
            localAuthentications = [Authetication]()
            for hash in array {
                if let auth = Authetication(json: hash) {
                    localAuthentications!.append(auth)
                }
            }
            
            //In this case the meta object should have the primaryResourceId key so we can look through all our auths and find the one we should be using for this session
            if let meta = json["meta"] as? [String: AnyObject], authID = meta["primaryResourceId"] as? Int{
                localAuthentication = localAuthentications!.filter{ $0.id == authID}.first
            }
        }
        
        //At this point we have collected all our values in the localy named ones so we can unwrap and assign them.  If they are non nil at this point we can fail
        if let u = localUser, let auth = localAuthentication, let auths = localAuthentications {
            self.user = u
            self.authentication = auth
            self.authentications = auths
        }
        else {
            self.user = RockauthUser()
            self.authentication = Authetication()
            self.authentications = [Authetication]()
            self.providerAuthentications = [ProviderAuthentication]()
            return nil
        }
        
        //pull the provider authentications out
        if let providerAuths = json["providerAuthentications"] as? [[String: AnyObject]] {
            var collector = [ProviderAuthentication]()
            for hash in providerAuths {
                if let auth = ProviderAuthentication(json: hash) {
                    collector.append(auth)
                }
            }
            self.providerAuthentications = collector
        }
        else{
            self.providerAuthentications = [ProviderAuthentication]()
        }
    }
}
{'fraction_non_alphanumeric': 0.07162849872773537, 'fraction_numerical': 0.0027989821882951653, 'mean_word_length': 2.134370015948963, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6257674', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1897, 'n_words': 784}