Skip to content

Commit

Permalink
Added OIDC & Access Token
Browse files Browse the repository at this point in the history
  • Loading branch information
TimmyRB committed Feb 8, 2024
1 parent 6a1b357 commit 1482185
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
18 changes: 17 additions & 1 deletion android/src/main/kotlin/com/jacobbrasil/snapkit/SnapkitPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import com.snap.creativekit.models.SnapContent
import com.snap.creativekit.models.SnapLiveCameraContent
import com.snap.creativekit.models.SnapPhotoContent
import com.snap.creativekit.models.SnapVideoContent
import com.snap.loginkit.AccessTokenResultCallback
import com.snap.loginkit.BitmojiQuery
import com.snap.loginkit.LoginResultCallback
import com.snap.loginkit.SnapLogin
import com.snap.loginkit.SnapLoginProvider
import com.snap.loginkit.UserDataQuery
import com.snap.loginkit.UserDataResultCallback
import com.snap.loginkit.exceptions.AccessTokenException
import com.snap.loginkit.exceptions.LoginException
import com.snap.loginkit.exceptions.UserDataException
import com.snap.loginkit.models.UserDataResult
Expand Down Expand Up @@ -108,7 +111,7 @@ class SnapkitPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
}
"getCurrentUser" -> {
val bitmojiQuery = BitmojiQuery.newBuilder().withAvatarId().withTwoDAvatarUrl().build()
val userDataQuery = UserDataQuery.newBuilder().withExternalId().withDisplayName().withBitmoji(bitmojiQuery).build()
val userDataQuery = UserDataQuery.newBuilder().withExternalId().withIdToken().withDisplayName().withBitmoji(bitmojiQuery).build()

SnapLoginProvider.get(requireActivity()).fetchUserData(userDataQuery, object: UserDataResultCallback {
override fun onSuccess(userDataResult: UserDataResult) {
Expand All @@ -120,6 +123,7 @@ class SnapkitPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
val meData = userDataResult.data!!.meData!!
val map: HashMap<String, String?> = HashMap<String, String?>()
map["externalId"] = meData.externalId
map["openIdToken"] = meData.idToken
map["displayName"] = meData.displayName
map["bitmoji2DAvatarUrl"] = meData.bitmojiData?.twoDAvatarUrl
map["bitmojiAvatarId"] = meData.bitmojiData?.avatarId
Expand All @@ -133,6 +137,18 @@ class SnapkitPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
}
})
}
"getAccessToken" -> {
SnapLoginProvider.get(requireActivity()).fetchAccessToken(object: AccessTokenResultCallback {
override fun onSuccess(token: String) {
result.success(token)
}

override fun onFailure(e: AccessTokenException) {
result.error("GetAccessTokenError", e.localizedMessage, e)
}

})
}
"logout" -> {
SnapLoginProvider.get(requireActivity()).clearToken()
result.success("Logout Success")
Expand Down
5 changes: 4 additions & 1 deletion ios/Classes/SnapkitPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ public class SnapkitPlugin: NSObject, FlutterPlugin {
})
break
case "getCurrentUser":
let queryBuilder = SCSDKUserDataQueryBuilder().withExternalId().withDisplayName().withBitmojiAvatarID().withBitmojiTwoDAvatarUrl()
let queryBuilder = SCSDKUserDataQueryBuilder().withExternalId().withDisplayName().withBitmojiAvatarID().withBitmojiTwoDAvatarUrl().withIdToken()
let query = queryBuilder.build()
SCSDKLoginClient.fetchUserData(with: query,
success: {(userdata: SCSDKUserData?, partialError: Error?) in
guard let data = userdata else { result(FlutterError(code: "GetUserError", message: "User data was null", details: nil)); return }

let map: [String: String?] = [
"externalId": data.externalID,
"openIdToken": data.idToken,
"displayName": data.displayName,
"bitmoji2DAvatarUrl": data.bitmojiTwoDAvatarUrl,
"bitmojiAvatarId": data.bitmojiAvatarID,
Expand All @@ -80,6 +81,8 @@ public class SnapkitPlugin: NSObject, FlutterPlugin {
}
})
break
case "getAccessToken":
result(SCSDKLoginClient.getAccessToken())
case "logout":
SCSDKLoginClient.clearToken()
result("Logout Success")
Expand Down
16 changes: 9 additions & 7 deletions lib/loginkit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,21 @@ class LoginKit {
}
}

if (data['externalId'] == null || data['displayName'] == null) {
if (data['externalId'] == null ||
data['displayName'] == null ||
data['openIdToken'] == null) {
throw LoginKitException('Invalid user data');
}

_currentUser = SnapchatUser(
data['externalId']!,
data['displayName']!,
data['bitmoji2DAvatarUrl'],
data['bitmojiAvatarId'],
);
_currentUser = SnapchatUser.fromJson(data);
_authEventsController.add(event);
}

/// Gets the Access Token for the Snapchat user.
Future<String?> getAccessToken() async {
return SnapkitPlatform.instance.getAccessToken();
}

/// Logs the Snapchat user out of your app.
Future<void> logout() async {
await SnapkitPlatform.instance.logout();
Expand Down
11 changes: 11 additions & 0 deletions lib/snapchat_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ class SnapchatUser {
/// This ID is unqiue to every user and unqiue to your app.
final String externalId;

/// The user's OIDC (OpenID Connect) token.
final String openIdToken;

/// The display name of the user.
/// This is not their username and can be changed by the user.
final String displayName;
Expand All @@ -16,8 +19,16 @@ class SnapchatUser {

const SnapchatUser(
this.externalId,
this.openIdToken,
this.displayName,
this.bitmoji2DAvatarUrl,
this.bitmojiAvatarId,
);

SnapchatUser.fromJson(Map<String, dynamic> json)
: externalId = json['externalId'],
openIdToken = json['openIdToken'],
displayName = json['displayName'],
bitmoji2DAvatarUrl = json['bitmoji2DAvatarUrl'],
bitmojiAvatarId = json['bitmojiAvatarId'];
}
5 changes: 5 additions & 0 deletions lib/snapkit_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class MethodChannelSnapkit extends SnapkitPlatform {
.invokeMapMethod<String, String?>('getCurrentUser');
}

@override
Future<String?> getAccessToken() async {
return await methodChannel.invokeMethod<String>('getAccessToken');
}

@override
Future<void> logout() async {
await methodChannel.invokeMethod<void>('logout');
Expand Down
4 changes: 4 additions & 0 deletions lib/snapkit_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ abstract class SnapkitPlatform extends PlatformInterface {
throw UnimplementedError('getCurrentUser() has not been implemented.');
}

Future<String?> getAccessToken() {
throw UnimplementedError('getAccessToken() has not been implemented.');
}

Future<void> logout() {
throw UnimplementedError('logout() has not been implemented.');
}
Expand Down
5 changes: 5 additions & 0 deletions test/snapkit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class MockSnapkitPlatform
Future<String?> getSnapSDKVersion() {
throw UnimplementedError();
}

@override
Future<String?> getAccessToken() {
throw UnimplementedError();
}
}

void main() {
Expand Down

0 comments on commit 1482185

Please sign in to comment.