SaltechSystems / couchbase_lite

Flutter plugin for the Community edition of Couchbase Lite. Couchbase Lite is an embedded lightweight, document-oriented (NoSQL), syncable database engine.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SessionAuthenticator not working when cookieName is not specified

matsunanaro opened this issue · comments

If a ReplicatorConfiguration class is set up with a SessionAuthenticator whose cookieName is not specified, the associated Replicator will fail with an error code "Unauthorized".

I believe the culprit is in the "guard" statement in the file ios/Classes/CBManager.swift:285

The guard statement will return nil because the json parameter can not be casted as a Dictionary<String, String> as it contains a NSNull object. For instance, when a SessionAuthenticator is initialized without a cookieName.

func inflateAuthenticator(json: Any?) throws -> Authenticator? {
        guard let map = json as? Dictionary<String,String> else {
            return nil
        }
        
        switch map["method"] {
        case "basic":
            guard let username = map["username"], let password = map["password"] else {
                throw CBManagerError.MissingArgument
            }
            
            return BasicAuthenticator(username: username, password: password)
        case "session":
            guard let sessionId = map["sessionId"] else {
                throw CBManagerError.MissingArgument
            }
            
            return SessionAuthenticator(sessionID: sessionId, cookieName: map["cookieName"])
        default:
            throw CBManagerError.IllegalArgument
        }
    }

Does it fail if you pass the cookieName? The guard statement should be fine as it is the json object for the SessionAuthenticator and should be {"method": "session", "sessionId": "yoursessionid", "cookieName": "yourcookiename or nil"}. Maybe you created a guest session and you don't allow guest login from sync gateway.

No, it doesn't fail when I pass the cookieName, e.g. "SyncGatewaySession". But it fails when the cookieName is omitted. Since cookieName in SessionAuthenticator is declared as a named parameter; in other words it is supposed to be optional. And I believe its counterpart in Swift is optional too. I am afraid it is a little bug. By the way, I think BasicAuthenticator would have the same issue for those accounts without passwords.

One more thing - when the cookieName was omitted, the json map in my debugger was shown as Dictionary<String, Any> since the cookieName was set as NSNull. Hope this would help.

That's definitely what is happening. Good catch!

For now pass the cookieName, I don't have time to spend on this at the moment but you're welcome to make the changes and do a pull request with the fix in place. If you do this it won't take long for me to push the changes to flutter pub but you can use your code with the fix in place until that is live.

No problem. ActuallyI am thinking about making a fix and do a pull request. Hopefully, it will be available sometime next week. In the meantime, I've worked around the issue by setting the cookieName to SyncGatewaySession. Thanks for your quick response.

This was merged, I just need to push a new release and then I will close the issue.

with basicauthenticator everything works fine ,but with sessionauthentication automatic sync dosent working. Is this issue related the same you mentioned above?

It could be related. Please try to call the SessionAuthenticator constructor with the cookieName specified.

It could be related. Please try to call the SessionAuthenticator constructor with the cookieName specified.

I haven't released the change to the plugin on pub.dev yet so you can reference this repo instead and it should solve your issue.

v2.7.1+1 has this fix in it