Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pin based NAU-login #358

Open
wants to merge 14 commits into
base: beta
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
46657183205085FC000E0D45 /* FilesPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46657182205085FC000E0D45 /* FilesPresenter.swift */; };
46CB896B204FB0FC0031151F /* SharesPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46CB896A204FB0FC0031151F /* SharesPresenter.swift */; };
46F87397204CC69500F1AF37 /* Server.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F87396204CC69500F1AF37 /* Server.swift */; };
7229DB3824EA9A490081AEF9 /* HACModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7229DB3724EA9A490081AEF9 /* HACModel.swift */; };
7229DB3A24EAF4700081AEF9 /* NAULoginController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7229DB3924EAF4700081AEF9 /* NAULoginController.swift */; };
7229DB4324EB303A0081AEF9 /* WifiGatewayIP.m in Sources */ = {isa = PBXBuildFile; fileRef = 7229DB4224EB303A0081AEF9 /* WifiGatewayIP.m */; };
7253F21824BDA89F0094C385 /* AudioThumbnailCollectionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7253F21624BDA89F0094C385 /* AudioThumbnailCollectionCell.swift */; };
7253F21924BDA89F0094C385 /* AudioThumbnailCollectionCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7253F21724BDA89F0094C385 /* AudioThumbnailCollectionCell.xib */; };
7253F21B24BDAF890094C385 /* AudioPlayerViewController+CollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7253F21A24BDAF890094C385 /* AudioPlayerViewController+CollectionView.swift */; };
Expand Down Expand Up @@ -215,6 +218,11 @@
46F87396204CC69500F1AF37 /* Server.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Server.swift; sourceTree = "<group>"; };
499AAFCDBDAB4C05C7264992 /* Pods-AmahiAnywhere.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AmahiAnywhere.release.xcconfig"; path = "Pods/Target Support Files/Pods-AmahiAnywhere/Pods-AmahiAnywhere.release.xcconfig"; sourceTree = "<group>"; };
665E1C7EF1F0DEBF3A9809B0 /* Pods-AmahiAnywhere.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AmahiAnywhere.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AmahiAnywhere/Pods-AmahiAnywhere.debug.xcconfig"; sourceTree = "<group>"; };
7229DB3724EA9A490081AEF9 /* HACModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HACModel.swift; sourceTree = "<group>"; };
7229DB3924EAF4700081AEF9 /* NAULoginController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NAULoginController.swift; sourceTree = "<group>"; };
7229DB4224EB303A0081AEF9 /* WifiGatewayIP.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WifiGatewayIP.m; sourceTree = "<group>"; };
7229DB4624EB31910081AEF9 /* WifiGatewayIPHelper.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = WifiGatewayIPHelper.c; sourceTree = "<group>"; };
723E3D2124EBA26600FFFA9A /* WifiGatewayIP.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WifiGatewayIP.h; sourceTree = "<group>"; };
7253F21624BDA89F0094C385 /* AudioThumbnailCollectionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioThumbnailCollectionCell.swift; sourceTree = "<group>"; };
7253F21724BDA89F0094C385 /* AudioThumbnailCollectionCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AudioThumbnailCollectionCell.xib; sourceTree = "<group>"; };
7253F21A24BDAF890094C385 /* AudioPlayerViewController+CollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AudioPlayerViewController+CollectionView.swift"; sourceTree = "<group>"; };
Expand All @@ -223,6 +231,7 @@
7270E662248D14A6001BE439 /* AudioPlayerQueueViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioPlayerQueueViewController.swift; sourceTree = "<group>"; };
7270E664248D448F001BE439 /* AudioPlayerViewController + PlayerQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AudioPlayerViewController + PlayerQueue.swift"; sourceTree = "<group>"; };
727A95DF24D2DA350057C27C /* UIImageExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIImageExtension.swift; sourceTree = "<group>"; };
7284755624F92C9A00DCB78B /* route.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = route.h; sourceTree = "<group>"; };
72BDA8B324A9ED6900B4469E /* QueueItemTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueItemTableViewCell.swift; sourceTree = "<group>"; };
72BDA8B424A9ED6900B4469E /* QueueItemTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = QueueItemTableViewCell.xib; sourceTree = "<group>"; };
72DAFE1B24B6276A00755E25 /* AudioPlayerDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioPlayerDataModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -390,6 +399,17 @@
path = Walkthrough;
sourceTree = "<group>";
};
723E3D2824EBA78500FFFA9A /* Gateway IP Utility */ = {
isa = PBXGroup;
children = (
7229DB4224EB303A0081AEF9 /* WifiGatewayIP.m */,
723E3D2124EBA26600FFFA9A /* WifiGatewayIP.h */,
7229DB4624EB31910081AEF9 /* WifiGatewayIPHelper.c */,
7284755624F92C9A00DCB78B /* route.h */,
);
path = "Gateway IP Utility";
sourceTree = "<group>";
};
7258718D2496BC6100AF0A61 /* Views */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -442,6 +462,7 @@
3B37EBAE22ED11650065CB77 /* Units.swift */,
9935DAB32301FA21004816E1 /* RecentFiles.swift */,
727A95DF24D2DA350057C27C /* UIImageExtension.swift */,
723E3D2824EBA78500FFFA9A /* Gateway IP Utility */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -531,6 +552,7 @@
children = (
FB71BE99201CB2FC0005492C /* LoginViewController.swift */,
C83C639C203992460089B9D8 /* LoginPresenter.swift */,
7229DB3924EAF4700081AEF9 /* NAULoginController.swift */,
);
path = Login;
sourceTree = "<group>";
Expand Down Expand Up @@ -562,6 +584,7 @@
9935DAAE2301ED86004816E1 /* RecentFile+CoreDataClass.swift */,
3B2805E32312CA1C0022ED48 /* RecentFile+CoreDataProperties.swift */,
72DAFE1B24B6276A00755E25 /* AudioPlayerDataModel.swift */,
7229DB3724EA9A490081AEF9 /* HACModel.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -893,6 +916,7 @@
4605964B204F35E9004FA066 /* ServerRoute.swift in Sources */,
3B4DB99722A0431100AF287B /* ServerCollectionViewCell.swift in Sources */,
3B33325622BAA2AF00BC61EB /* FilesCollectionFooterView.swift in Sources */,
7229DB3A24EAF4700081AEF9 /* NAULoginController.swift in Sources */,
C8DEBFAB20BAE1D300B09AED /* ConnectionModeManager.swift in Sources */,
C8ED025920B3E30300015846 /* URL.swift in Sources */,
C8DA5B1A20D5DC2200F5A527 /* DownloadService.swift in Sources */,
Expand Down Expand Up @@ -953,6 +977,7 @@
3B2805E42312CA1C0022ED48 /* RecentFile+CoreDataProperties.swift in Sources */,
3B3F43DF22FECF7300BB42B0 /* DissmissAnimator.swift in Sources */,
725871932496C65300AF0A61 /* PlayerQueueContainerView.swift in Sources */,
7229DB4324EB303A0081AEF9 /* WifiGatewayIP.m in Sources */,
C83C637E2038D5150089B9D8 /* StringIdentifiers.swift in Sources */,
3B33325022BAA26600BC61EB /* FilesGridCollectionCell.swift in Sources */,
3BCAFBA322C537DE0044057E /* DownloadsGridCollectionCell.swift in Sources */,
Expand All @@ -972,6 +997,7 @@
46098D08204FFFBE00752EB6 /* ServerApi.swift in Sources */,
3B33325422BAA29700BC61EB /* FilesCollectionHeaderView.swift in Sources */,
3B3F43E122FECF9300BB42B0 /* Interactor.swift in Sources */,
7229DB3824EA9A490081AEF9 /* HACModel.swift in Sources */,
C8D86160209490230014D8AC /* WebViewController.swift in Sources */,
3BF1E81A22D0288700C531CD /* OfflineFileIndexes.swift in Sources */,
7253F21B24BDAF890094C385 /* AudioPlayerViewController+CollectionView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
//

#import "MobileVLCKit/MobileVLCKit.h"
#import "WifiGatewayIP.h"
50 changes: 39 additions & 11 deletions AmahiAnywhere/AmahiAnywhere/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

var orientationLock = UIInterfaceOrientationMask.all
let mainStoryboard: UIStoryboard = UIStoryboard(name: StoryBoardIdentifiers.main, bundle: nil)

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
Expand Down Expand Up @@ -92,9 +93,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
//LocalStorage.shared.delete(key: "walkthrough")

self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: StoryBoardIdentifiers.main, bundle: nil)
var initialViewController: UIViewController? = nil

if LocalStorage.shared.contains(key: PersistenceIdentifiers.accessToken) {

if useCastContainerViewController {
Expand All @@ -111,16 +111,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
} else {
if LocalStorage.shared.contains(key: "walkthrough"){
// User already completed the onboarding
initialViewController = mainStoryboard.instantiateInitialViewController()
}else{
// User didn't complete the onboarding yet
initialViewController = mainStoryboard.instantiateViewController(withIdentifier: StoryBoardIdentifiers.walktrhoughViewController)
}else if let hac = LocalStorage.shared.getDictionary(for: PersistenceIdentifiers.hdaAuthCache) {
for ip in hac.keys{
if let serverDetails = hac[ip]{
let serverObject = HDAAuthCache(from: serverDetails)
if let authToken = serverObject.authToken{
ServerApi.shared?.auth_token = authToken
let address = ApiEndPoints.getNauAddress(from: ip)
ServerApi.shared?.serverAddress = address
let rootVC = mainStoryboard.instantiateViewController(withIdentifier: "RootVC") as! RootContainerViewController
rootVC.isNAULogin = true
LocalStorage.shared.persist(true, for: PersistenceIdentifiers.isNAULogin)
self.window?.rootViewController = rootVC
self.window?.makeKeyAndVisible()
}
}
}
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
if window?.rootViewController == nil{
//none of the cached server had an auth_token fallback to login flow.
installLoginViewController()
}
}

else {
installLoginViewController()
}

// Setting default layout view value
Expand Down Expand Up @@ -153,6 +167,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}

func installLoginViewController(){
var initialViewController : UIViewController? = nil

if LocalStorage.shared.contains(key: "walkthrough"){
// User already completed the onboarding
initialViewController = mainStoryboard.instantiateInitialViewController()
}else{
// User didn't complete the onboarding yet
initialViewController = mainStoryboard.instantiateViewController(withIdentifier: StoryBoardIdentifiers.walktrhoughViewController)
}
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}

func setupMiniController(){

self.window = UIWindow(frame: UIScreen.main.bounds)
Expand Down
30 changes: 29 additions & 1 deletion AmahiAnywhere/AmahiAnywhere/Data/Local/LocalStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,29 @@ final class LocalStorage: NSObject {
UserDefaults.standard.setValue(string, forKey: key);
UserDefaults.standard.synchronize();
}

public func persistDictionary(_ dictionary:[String:[String:String]],for key: String){
UserDefaults.standard.set(dictionary, forKey: key)
}

public func getDictionary(for key: String)->[String:[String:String]]?{
return UserDefaults.standard.object(forKey: key) as? [String:[String:String]]
}


public func getString(key: String!) -> String? {
UserDefaults.standard.synchronize()
return UserDefaults.standard.value(forKey: key) as? String;
}

public func getBool(_ key:String) -> Bool{
return UserDefaults.standard.bool(forKey: key)
}

public func persist(_ bool:Bool, for key:String){
UserDefaults.standard.set(bool, forKey: key)
}

public func contains(key: String!) -> Bool{
return UserDefaults.standard.object(forKey: key) != nil
}
Expand All @@ -37,9 +54,20 @@ final class LocalStorage: NSObject {
}

public func logout(_ complete: () -> Void){
var hac = getDictionary(for: PersistenceIdentifiers.hdaAuthCache)
clearAll()
persistString(string: "completed", key: "walkthrough")
complete();

//wiping auth_token for cached HDA
if hac != nil{
for ip in hac!.keys{
hac![ip]?[HACIdentifiers.auth_token] = ""
}
persistDictionary(hac!, for: PersistenceIdentifiers.hdaAuthCache)
}

RecentsDatabaseHelper.shareInstance.clearAllRecents()
complete()
}

public func clearAll(){
Expand Down
46 changes: 46 additions & 0 deletions AmahiAnywhere/AmahiAnywhere/Data/Local/Models/HACModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// HACModel.swift
// AmahiAnywhere
//
// Created by Shresth Pratap Singh on 17/08/20.
// Copyright © 2020 Amahi. All rights reserved.
//

import Foundation
import EVReflection

struct HACIdentifiers{
static let server_ip = "server_ip"
static let session_token = "session_token"
static let auth_token = "auth_token"
static let server_address = "server_address"
}

struct HDAAuthCache{

var serverLocalIP:String?
var sessionToken:String?
var authToken:String?
var serverAddress:String?

init(from dictionary:[String:String]) {
serverLocalIP = (dictionary[HACIdentifiers.server_ip] == "") ? nil : dictionary[HACIdentifiers.server_ip]
sessionToken = (dictionary[HACIdentifiers.session_token] == "") ? nil : dictionary[HACIdentifiers.session_token]
authToken = (dictionary[HACIdentifiers.auth_token] == "") ? nil : dictionary[HACIdentifiers.auth_token]
serverAddress = (dictionary[HACIdentifiers.server_address] == "") ? nil : dictionary[HACIdentifiers.server_address]
}

init(ip:String?, sessionToken:String?, authToken:String?, serverAddress:String?) {
self.serverLocalIP = ip
self.sessionToken = sessionToken
self.authToken = authToken
self.serverAddress = serverAddress
}

var toDictionary:[String:String]{
return [HACIdentifiers.server_ip : serverLocalIP ?? "",
HACIdentifiers.session_token : sessionToken ?? "",
HACIdentifiers.auth_token : authToken ?? "",
HACIdentifiers.server_address : serverAddress ?? ""]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ struct PersistenceIdentifiers {

static let accessToken = "access_token"
static let prefConnection = "pref_connection"
static let hdaAuthCache = "hda_auth_cache"
static let isNAULogin = "nau_logged_in"
}
11 changes: 11 additions & 0 deletions AmahiAnywhere/AmahiAnywhere/Data/Local/RecentsDatabaseHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@ class RecentsDatabaseHelper {
}
}

func clearAllRecents(){
let entityFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "RecentFile")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: entityFetch)
do{
try context.execute(deleteRequest)
try context.save()
}catch(let error){
AmahiLogger.log("Failed to clear Recents Files due to error : " + error.localizedDescription)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class AmahiApi {
headers: headers, completion: completion)
}

func login(pin:String,url:URL,compltion: @escaping (_ success:Bool,_ authToken:String?)->Void){
Network.shared.login(pin: pin, url: url, completion: compltion)
}

func getServers(completion: @escaping (_ servers: [Server]?) -> Void ) {
Network.shared.request(ApiEndPoints.fetchServers(), completion: completion)
}
Expand Down
28 changes: 14 additions & 14 deletions AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import Alamofire
import EVReflection

class ServerApi {
public static var shared: ServerApi?
public static var shared: ServerApi? = ServerApi()

private var server: Server!
private var server: Server?
private var serverRoute: ServerRoute?
private var serverAddress: String?
var serverAddress: String?
public var auth_token: String? // if nil -> server is default welcome to amahi

private init() {}
Expand Down Expand Up @@ -44,9 +44,9 @@ class ServerApi {

public func getServerHeaders() -> HTTPHeaders {
if let authToken = auth_token{
return [ "Session": server.session_token!, "Authorization": authToken]
return ["Session": (server?.session_token ?? ""), "Authorization": authToken]
}else{
return [ "Session": server.session_token! ]
return [ "Session": server?.session_token ?? "" ]
}
}

Expand Down Expand Up @@ -102,13 +102,13 @@ class ServerApi {
}

func getShares(completion: @escaping (_ serverShares: [ServerShare]?) -> Void ) {
if serverRoute == nil{
completion(nil)
return
}

if serverAddress == nil{
serverAddress = ConnectionModeManager.shared.currentConnectionBaseURL(serverRoute: serverRoute!)
if !LocalStorage.shared.getBool(PersistenceIdentifiers.isNAULogin){
if serverRoute == nil{
completion(nil)
}
if serverAddress == nil{
serverAddress = ConnectionModeManager.shared.currentConnectionBaseURL(serverRoute: serverRoute!)
}
}

Network.shared.request(ApiEndPoints.getServerShares(serverAddress), headers: getServerHeaders(), completion: completion)
Expand Down Expand Up @@ -181,7 +181,7 @@ class ServerApi {
URLQueryItem(name: "s", value: file.parentShare!.name),
URLQueryItem(name: "p", value: file.getPath()),
URLQueryItem(name: "mtime", value: String(file.getLastModifiedEpoch())),
URLQueryItem(name: "session", value: server.session_token),
URLQueryItem(name: "session", value: (server?.session_token)),
]

if let authToken = auth_token{
Expand All @@ -200,7 +200,7 @@ class ServerApi {
URLQueryItem(name: "s", value: file.parentShare!.name),
URLQueryItem(name: "p", value: file.getPath()),
URLQueryItem(name: "mtime", value: String(file.getLastModifiedEpoch())),
URLQueryItem(name: "session", value: server.session_token)
URLQueryItem(name: "session", value: (server?.session_token ?? ""))
]

if let authToken = auth_token{
Expand Down
8 changes: 8 additions & 0 deletions AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiEndPoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ struct ApiEndPoints {
return "\(serverUrl!)/shares"
}

static func getNauAuthUrl(from ip:String)->URL?{
return URL(string: "http://" + ip + ":4563/auth")
}

static func getNauAddress(from ip:String) -> String{
return "http://"+ip+":4563"
}

static func authenticateServerWithPin(_ serverUrl: String!) -> String! {
return "\(serverUrl!)/auth"
}
Expand Down
Loading