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

change issuer to constant #168

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 31 additions & 16 deletions IBMCloudAppIDTests/ConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,52 @@ import XCTest
import BMSCore
@testable import IBMCloudAppID

internal var newRegion = "https://us-south.appid.cloud.ibm.com" //full url with https
internal var oldRegion = ".ng.bluemix.net"
internal var customRegion = ".custom"



public class ConfigTests: XCTestCase {

func testGetServerUrl() {

func testConfig() {
AppID.sharedInstance = AppID()

// no region and tenant
let appid = AppID.sharedInstance
XCTAssertEqual("https://appid-oauth", Config.getServerUrl(appId: appid))
XCTAssertEqual("appid-oauth", Config.getIssuer(appId: appid))

// with region and tenant
appid.initialize(tenantId: "sometenant", region: "https://region")
XCTAssertEqual("https://region/oauth/v3/sometenant", Config.getServerUrl(appId: appid))

XCTAssertEqual("https://region/oauth/v3/sometenant/publickeys", Config.getPublicKeyEndpoint(appId: appid))
XCTAssertEqual("https://region/api/v1/", Config.getAttributesUrl(appId: appid))

appid.initialize(tenantId: "sometenant", region: newRegion)
XCTAssertEqual( newRegion + "/oauth/v3/sometenant", Config.getServerUrl(appId: appid))

XCTAssertEqual("region", Config.getIssuer(appId: appid))
XCTAssertEqual( newRegion + "/oauth/v3/sometenant/publickeys", Config.getPublicKeyEndpoint(appId: appid))
XCTAssertEqual( newRegion + "/api/v1/", Config.getAttributesUrl(appId: appid))
XCTAssertEqual("appid-oauth.ng.bluemix.net", Config.getIssuer(appId: appid))

// with OLD .region and tenant
appid.initialize(tenantId: "sometenant", region: ".region")
XCTAssertEqual("https://appid-oauth.region/oauth/v3/sometenant", Config.getServerUrl(appId: appid))
XCTAssertEqual("https://appid-oauth.region/oauth/v3/sometenant/publickeys", Config.getPublicKeyEndpoint(appId: appid))
XCTAssertEqual("https://appid-profiles.region/api/v1/", Config.getAttributesUrl(appId: appid))

XCTAssertEqual("appid-oauth.region", Config.getIssuer(appId: appid))
appid.initialize(tenantId: "sometenant", region: oldRegion)
XCTAssertEqual("https://appid-oauth" + oldRegion + "/oauth/v3/sometenant", Config.getServerUrl(appId: appid))
XCTAssertEqual("https://appid-oauth" + oldRegion + "/oauth/v3/sometenant/publickeys", Config.getPublicKeyEndpoint(appId: appid))
XCTAssertEqual("https://appid-profiles" + oldRegion + "/api/v1/", Config.getAttributesUrl(appId: appid))
XCTAssertEqual("appid-oauth" + oldRegion, Config.getIssuer(appId: appid))

//with custom region
appid.initialize(tenantId: "sometenant", region: customRegion)
XCTAssertEqual("https://appid-oauth" + customRegion + "/oauth/v3/sometenant", Config.getServerUrl(appId: appid))
XCTAssertEqual("https://appid-oauth" + customRegion + "/oauth/v3/sometenant/publickeys", Config.getPublicKeyEndpoint(appId: appid))
XCTAssertEqual("https://appid-profiles" + customRegion + "/api/v1/", Config.getAttributesUrl(appId: appid))
XCTAssertEqual("appid-oauth" + customRegion, Config.getIssuer(appId: appid))

// with overrideserverhost
AppID.overrideServerHost = "somehost/"
XCTAssertEqual("somehost/sometenant", Config.getServerUrl(appId: appid))


XCTAssertEqual("somehost/", Config.getIssuer(appId: appid))
}



}
66 changes: 51 additions & 15 deletions Source/IBMCloudAppID/internal/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,92 @@ import Foundation
import BMSCore

internal class Config {

private static var oauthEndpoint = "/oauth/v3/"
private static var attributesEndpoint = "/api/v1/"
private static var serverUrlPrefix = "https://appid-oauth"
private static var attributesUrlPrefix = "https://appid-profiles"
private static var publicKeysEndpoint = "/publickeys"
private static var urlProtocol = "http"



private static var REGION_US_SOUTH_OLD = ".ng.bluemix.net";
private static var REGION_US_EAST_OLD = ".us-east.bluemix.net";
private static var REGION_UK_OLD = ".eu-gb.bluemix.net";
private static var REGION_SYDNEY_OLD = ".au-syd.bluemix.net";
private static var REGION_GERMANY_OLD = ".eu-de.bluemix.net";
private static var REGION_TOKYO_OLD = ".jp-tok.bluemix.net";

internal static let logger = Logger.logger(name: AppIDConstants.ConfigLoggerName)

internal static func getServerUrl(appId:AppID) -> String {

guard let region = appId.region, let tenant = appId.tenantId else {
logger.error(message: "Could not set server url properly, no tenantId or no region set")
return serverUrlPrefix
}

var serverUrl = region.starts(with: urlProtocol) ? region + oauthEndpoint : serverUrlPrefix + region + oauthEndpoint
if let overrideServerHost = AppID.overrideServerHost {
serverUrl = overrideServerHost
}

serverUrl = serverUrl + tenant
return serverUrl
}

internal static func getAttributesUrl(appId:AppID) -> String {

guard let region = appId.region else {
logger.error(message: "Could not set server url properly, no region set")
return serverUrlPrefix
}

var attributesUrl = region.starts(with: urlProtocol) ? region + attributesEndpoint : attributesUrlPrefix + region + attributesEndpoint
if let overrideHost = AppID.overrideAttributesHost {
attributesUrl = overrideHost
}

return attributesUrl
}

internal static func getPublicKeyEndpoint(appId: AppID) -> String {
return getServerUrl(appId:appId) + publicKeysEndpoint
}

internal static func getIssuer(appId: AppID) -> String? {
guard let url = URL(string: getServerUrl(appId:appId)) else {
return nil

if let overrideServerHost = AppID.overrideServerHost {
return URL(string: overrideServerHost)?.host ?? AppID.overrideServerHost
}
return url.host

let region = appId.region ?? ""
let issuer = region.range(of:"cloud.ibm.com") == nil ? getServerUrl(appId:appId) : serverUrlPrefix + suffixFromRegion(region: region)

return URL(string: issuer)?.host ?? issuer
}

internal static func suffixFromRegion(region: String) -> String {
switch region {
case AppID.REGION_UK_STAGE1:
return ".stage1" + REGION_UK_OLD;
case AppID.REGION_US_SOUTH_STAGE1:
return ".stage1" + REGION_US_SOUTH_OLD;
case AppID.REGION_US_SOUTH:
return REGION_US_SOUTH_OLD;
case AppID.REGION_UK:
return REGION_UK_OLD;
case AppID.REGION_SYDNEY:
return REGION_SYDNEY_OLD;
case AppID.REGION_GERMANY:
return REGION_GERMANY_OLD;
case AppID.REGION_US_EAST:
return REGION_US_EAST_OLD;
case AppID.REGION_TOKYO:
return REGION_TOKYO_OLD;
default:
return region;
}

}

}