Skip to content

Commit

Permalink
Merge pull request #188 from line/feature/multi-linked-bot
Browse files Browse the repository at this point in the history
Add promptBotID field for multi linked bot feature
  • Loading branch information
onevcat authored Jun 28, 2023
2 parents 16537b4 + 1a7566d commit fb17319
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
2 changes: 0 additions & 2 deletions LineSDK/LineSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,6 @@
4BBAFC4A2101CE8100E7BFF6 /* Resource.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = Resource.bundle; sourceTree = "<group>"; };
4BBAFC4F2101D31300E7BFF6 /* ConstantTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConstantTests.swift; sourceTree = "<group>"; };
4BBEA992212EAF9F00858627 /* LineSDKObjC.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = LineSDKObjC.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4BBEA994212EAF9F00858627 /* LineSDKObjC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LineSDKObjC.h; sourceTree = "<group>"; };
4BBEA995212EAF9F00858627 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4BBEA99E212EAFBB00858627 /* LineSDKObjCInterfaceTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LineSDKObjCInterfaceTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4BBEA9A2212EAFBB00858627 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1473,7 +1472,6 @@
4BC9B2E5212FC9E30071C736 /* Messaging */,
4B2D14FB212F9FA5000DD5BE /* Networking */,
4BC9B2D7212FAD9E0071C736 /* Utils */,
4BBEA994212EAF9F00858627 /* LineSDKObjC.h */,
4BBEA995212EAF9F00858627 /* Info.plist */,
);
path = LineSDKObjC;
Expand Down
4 changes: 4 additions & 0 deletions LineSDK/LineSDK/Login/LoginManagerParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ extension LoginManager {
/// LINE and the login screen are always displayed in the user's device language.
///
public var preferredWebPageLanguage: WebPageLanguage? = nil

/// :nodoc:
// Not yet public available.
public var promptBotID: String? = nil

/// Sets the nonce value for ID token verification. This value is used when requesting user authorization
/// with `.openID` permission to prevent replay attacks to your backend server. If not set, LINE SDK will
Expand Down
31 changes: 18 additions & 13 deletions LineSDK/LineSDK/Login/LoginProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class LoginProcess {
let botPrompt: LoginManager.BotPrompt?
let preferredWebPageLanguage: LoginManager.WebPageLanguage?
let onlyWebLogin: Bool
let promptBotID: String?
}

/// Observes application switching to foreground.
Expand Down Expand Up @@ -131,24 +132,25 @@ public class LoginProcess {

func start() {
let parameters = FlowParameters(
channelID: self.configuration.channelID,
universalLinkURL: self.configuration.universalLinkURL,
scopes: self.scopes,
pkce: self.pkce,
processID: self.processID,
nonce: self.IDTokenNonce,
botPrompt: self.parameters.botPromptStyle,
preferredWebPageLanguage: self.parameters.preferredWebPageLanguage,
onlyWebLogin: self.parameters.onlyWebLogin
channelID: configuration.channelID,
universalLinkURL: configuration.universalLinkURL,
scopes: scopes,
pkce: pkce,
processID: processID,
nonce: IDTokenNonce,
botPrompt: parameters.botPromptStyle,
preferredWebPageLanguage: parameters.preferredWebPageLanguage,
onlyWebLogin: parameters.onlyWebLogin,
promptBotID: parameters.promptBotID
)
#if targetEnvironment(macCatalyst)
// On macCatalyst, we only support web login
self.startWebLoginFlow(parameters)
startWebLoginFlow(parameters)
#else
if self.parameters.onlyWebLogin {
self.startWebLoginFlow(parameters)
if parameters.onlyWebLogin {
startWebLoginFlow(parameters)
} else {
self.startAppUniversalLinkFlow(parameters)
startAppUniversalLinkFlow(parameters)
}
#endif
}
Expand Down Expand Up @@ -428,6 +430,9 @@ extension String {
if let botPrompt = parameter.botPrompt {
parameters["bot_prompt"] = botPrompt.rawValue
}
if let promptBotID = parameter.promptBotID {
parameters["prompt_bot_id"] = promptBotID
}
let base = URL(string: "/oauth2/v2.1/authorize/consent")!
let encoder = URLQueryEncoder(parameters: parameters)
return encoder.encoded(for: base).absoluteString
Expand Down
5 changes: 5 additions & 0 deletions LineSDK/LineSDKObjC/Login/LineSDKLoginManagerParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public class LineSDKLoginManagerParameters: NSObject {
get { return _value.botPromptStyle.map(LineSDKLoginManagerBotPrompt.init) }
set { _value.botPromptStyle = newValue?._value }
}

public var promptBotID: String? {
get { return _value.promptBotID }
set { _value.promptBotID = newValue }
}

public var preferredWebPageLanguage: String? {
get { return _value.preferredWebPageLanguage?.rawValue }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ - (void)testLoginManagerParametersInterface {
param.botPromptStyle = [LineSDKLoginManagerBotPrompt normal];
param.preferredWebPageLanguage = @"ja";
param.IDTokenNonce = @"test";
param.promptBotID = @"@abc123";

XCTAssertTrue([param onlyWebLogin]);
XCTAssertTrue([[param.botPromptStyle rawValue] isEqualToString: @"normal"]);
Expand Down
46 changes: 39 additions & 7 deletions LineSDK/LineSDKTests/Login/LoginFlowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class LoginFlowTests: XCTestCase, ViewControllerCompatibleTest {
nonce: "kkk",
botPrompt: .normal,
preferredWebPageLanguage: nil,
onlyWebLogin: false
onlyWebLogin: false,
promptBotID: nil
)

let parameterWithLanguage = LoginProcess.FlowParameters(
Expand All @@ -47,7 +48,8 @@ class LoginFlowTests: XCTestCase, ViewControllerCompatibleTest {
nonce: "kkk",
botPrompt: .normal,
preferredWebPageLanguage: .chineseSimplified,
onlyWebLogin: false
onlyWebLogin: false,
promptBotID: nil
)

let parameterWithOnlyWebLogin = LoginProcess.FlowParameters(
Expand All @@ -59,7 +61,21 @@ class LoginFlowTests: XCTestCase, ViewControllerCompatibleTest {
nonce: "kkk",
botPrompt: .normal,
preferredWebPageLanguage: nil,
onlyWebLogin: true
onlyWebLogin: true,
promptBotID: nil
)

let parameterWithPromptBotID = LoginProcess.FlowParameters(
channelID: "123",
universalLinkURL: nil,
scopes: [.profile, .openID],
pkce: PKCE(),
processID: "abc",
nonce: "kkk",
botPrompt: .normal,
preferredWebPageLanguage: nil,
onlyWebLogin: false,
promptBotID: "@abc123"
)

// Login URL has a double escaped query.
Expand All @@ -73,7 +89,7 @@ class LoginFlowTests: XCTestCase, ViewControllerCompatibleTest {

let components = URLComponents(url: result, resolvingAgainstBaseURL: false)
let items = components!.queryItems!
XCTAssertEqual(items.count, 2)
XCTAssertEqual(items.count, ["loginChannelId", "returnUri"].count)

var item: URLQueryItem

Expand All @@ -98,7 +114,7 @@ class LoginFlowTests: XCTestCase, ViewControllerCompatibleTest {

let components = URLComponents(url: result, resolvingAgainstBaseURL: false)
let items = components!.queryItems!
XCTAssertEqual(items.count, 3)
XCTAssertEqual(items.count, ["loginChannelId", "returnUri", "ui_locales"].count)

var item: URLQueryItem

Expand Down Expand Up @@ -126,7 +142,7 @@ class LoginFlowTests: XCTestCase, ViewControllerCompatibleTest {

let components = URLComponents(url: result, resolvingAgainstBaseURL: false)
let items = components!.queryItems!
XCTAssertEqual(items.count, 3)
XCTAssertEqual(items.count, ["loginChannelId", "returnUri", "disable_ios_auto_login"].count)

var item: URLQueryItem

Expand All @@ -139,6 +155,22 @@ class LoginFlowTests: XCTestCase, ViewControllerCompatibleTest {
item = items.first { $0.name == "disable_ios_auto_login" }!
XCTAssertEqual(item.value, "true")
}

func testLoginQueryWithPromptBotID() {
let baseURL = URL(string: Constant.lineWebAuthUniversalURL)!
let result = baseURL.appendedLoginQuery(parameterWithPromptBotID)

let urlString = result.absoluteString.removingPercentEncoding
XCTAssertNotNil(urlString)

let components = URLComponents(url: result, resolvingAgainstBaseURL: false)
let items = components!.queryItems!
XCTAssertEqual(items.count, ["loginChannelId", "returnUri"].count)

let item = items.first { $0.name == "returnUri" }!
XCTAssertNotEqual(item.value, item.value?.removingPercentEncoding)
XCTAssertTrue(item.value!.removingPercentEncoding!.contains("prompt_bot_id=@abc123"))
}

// URL Scheme has a triple escaped query.
func testURLSchemeQueryEncode() {
Expand All @@ -150,7 +182,7 @@ class LoginFlowTests: XCTestCase, ViewControllerCompatibleTest {

let components = URLComponents(url: result, resolvingAgainstBaseURL: false)
let items = components!.queryItems!
XCTAssertEqual(items.count, 1)
XCTAssertEqual(items.count, ["loginChannelId"].count)

let item = items.first { $0.name == "loginUrl" }!
XCTAssertNotEqual(item.value, item.value?.removingPercentEncoding)
Expand Down

0 comments on commit fb17319

Please sign in to comment.