Commit d39eb205 authored by Tomas Vik's avatar Tomas Vik
Browse files

Connecting the iOS app to local webapp

This MR contains several changes in the code replacing hardcoded `gitter.im` references with using URL from config. There is a change to Transport security settings, allowing HTTP connections to `localhost:5000`. Lastly, the updated README explains the necessary steps for connecting to the local webapp.

Thank you @MadLittleMods for unblocking me with the transport security settings, saving me hours if not days of struggle. 

https://gitlab.com/gitlab-org/gitter/gitter-ios-app/merge_requests/17
parent e2beb47c
......@@ -2,7 +2,7 @@ import Foundation
class LoginData {
private let service = "Gitter: https://gitter.im"
private let appSettings = TRAppSettings.sharedInstance()
private let keychain = KeychainItemWrapper(identifier: "OAuth", accessGroup: nil)
private let userDefaults = UserDefaults.standard
......@@ -37,7 +37,7 @@ class LoginData {
}
private func setAccessToken(_ accessToken: String) {
keychain?.setObject(service, forKey: kSecAttrService)
keychain?.setObject(appSettings!.keychainItemName(), forKey: kSecAttrService)
keychain?.setObject("OAuth", forKey: kSecAttrAccount)
keychain?.setObject(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, forKey: kSecAttrAccessible)
keychain?.setObject(generateGTMOAuth2String(accessToken), forKey: kSecValueData)
......@@ -52,8 +52,7 @@ class LoginData {
}
private func generateGTMOAuth2String(_ accessToken: String) -> String {
let scope = "https://gitter.im/login/oauth/callback"
return "access_token=\(accessToken)&scope=\(urlEncode(scope))&serviceProvider=\(urlEncode(service))"
return "access_token=\(accessToken)&scope=\(urlEncode(appSettings!.oauthScope()))&serviceProvider=\(urlEncode(appSettings!.keychainItemName()))"
}
private func urlEncode(_ string: String) -> String {
......
......@@ -13,7 +13,6 @@ class LoginOauthViewController: UIViewController, WKNavigationDelegate {
delegate:nil,
delegateQueue:OperationQueue.main
)
private let host = "https://gitter.im"
override func viewDidLoad() {
super.viewDidLoad()
......@@ -69,7 +68,7 @@ class LoginOauthViewController: UIViewController, WKNavigationDelegate {
}
private func getAuthorisationUrl() -> URL {
var components = URLComponents(string: "\(host)/login/oauth/authorize")!
var components = URLComponents(string: "\(appSettings!.baseServerURLString()!)/login/oauth/authorize")!
var queryItems = [
URLQueryItem(name: "client_id", value: appSettings!.clientID()),
URLQueryItem(name: "redirect_uri", value: appSettings!.oauthScope()),
......@@ -100,7 +99,7 @@ class LoginOauthViewController: UIViewController, WKNavigationDelegate {
"code": code
], options: .prettyPrinted)
var request = URLRequest(url: URL(string: "\(host)/login/oauth/token")!)
var request = URLRequest(url: URL(string: "\(appSettings!.baseServerURLString()!)/login/oauth/token")!)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.httpMethod = "POST"
......
......@@ -91,5 +91,11 @@
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
<!-- allow connections to localhost over insecure HTTP (enables local development) -->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
</dict>
</plist>
......@@ -6,15 +6,15 @@
<string>https://gitter.im</string>
<key>TroupeAPIURL</key>
<string>https://api.gitter.im</string>
<key>TroupeWSURL</key>
<string>wss://ws.gitter.im/bayeux</string>
<key>TroupeOAuthRedirectURL</key>
<string>https://gitter.im/login/oauth/callback</string>
<key>TroupeOAuthAuthURL</key>
<string>/login/oauth/authorize</string>
<key>TroupeOAuthKeychainItemName</key>
<string>Gitter: </string>
<key>TroupeOAuthRedirectURL</key>
<string>https://gitter.im/login/oauth/callback</string>
<key>TroupeOAuthTokenURL</key>
<string>/login/oauth/token</string>
<key>TroupeWSURL</key>
<string>wss://ws.gitter.im/bayeux</string>
</dict>
</plist>
......@@ -13,7 +13,7 @@ Room list | Chat view
- This app is using Swift 5
### Setup
<span id="setup"></setup>
1. Clone and setup the [`webapp`](https://gitlab.com/gitlab-org/gitter/webapp) project
- At a very minimum, `npm install`. You don't necessarily need to run the webapp locally and can skip the Docker stuff
......@@ -26,16 +26,60 @@ Room list | Chat view
1. Open in Xcode: `open Gitter.xcworkspace`
## Run against localhost
In `Edit Scheme...` > `Arguments` > `Arguments Passed On Launch` add the following:
1. `-TroupeURL`
1. `http://localhost:5000`
1. `-TroupeWSURL`
1. `ws://localhost:5000/faye`
1. If you need to sign in, update your OAuth secrets in `Gitter/GitterSecrets-Dev.plist` with something from your local instance
## Run against local webapp
Follow the [Setup instructions](#setup) above, but compile the webapp assets by running `NODE_ENV=dev npm run build-ios-assets` in the `webapp` project. The **`dev` environment is important**.
Change the following properties in `Gitter/GitterDefaults.plist`:
```xml
<key>TroupeURL</key>
<string>http://localhost:5000</string>
<key>TroupeAPIURL</key>
<string>http://localhost:5000/api</string>
<key>TroupeWSURL</key>
<!-- the ws: means insecure protocol (as opposed to wss:) -->
<string>ws://localhost:5000/bayeux</string>
<key>TroupeOAuthRedirectURL</key>
<string>http://localhost:5000/login/oauth/callback</string>
```
*(You can paste this snippet over the current properties)*
### Set up the local OAuth
Create OAuth client for the iOS by running:
```
$ mongo gitter
> db.oauthclients.update(
{ clientKey: "ios-dev" },
{
clientKey: "ios-dev",
clientSecret: "dev-abcdef-whatevertoken123",
name: "Gitter iOS App (dev)",
tag: "ios-dev",
registeredRedirectUri: "http://localhost:5000/login/oauth/callback",
canSkipAuthorization: true
},
{ upsert: true }
);
```
Then you can use following snippet as `Gitter/GitterSecrets-Dev.plist`
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>OAuthClientId</key>
<string>ios-dev</string>
<key>OAuthClientSecret</key>
<string>dev-abcdef-whatevertoken123</string>
<key>OAuthCallback</key>
<string>http://localhost:5000/login/oauth/callback</string>
</dict>
</plist>
```
## Developer and debugging notes
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment