add Oauth link

Hi,

In my code I'm trying to create a new user, and I also want to add a new Oauth link, because our users connect to the community

using Oauth. I'm at the point in which I created the user and I want to add the new OAuthLink, but it needs a clientTypeclientUserId and a userId.

    public class OAuthLink
    {
        public OAuthLink(string clientType, string clientUserId, int userId);

        public string ClientType { get; set; }
        public string ClientUserId { get; set; }
        public int UserId { get; set; }
    }

I have the clientType and the userId, but how do I get a new clientUserId so it won't generate a duplicate in the community, or better yet how are clientUserId's generated?

            //the code I'm trying to use
            OAuthLink newLink = new OAuthLink(ClientType, userID, clientUserID);
            _OAuthService.AddOAuthLink(newLink);

Regards,

Silviu 

Parents Reply Children
  • So here is my  implementation on the above mention plugin.. I know I am missing something but let me know 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    using System.ServiceModel.Security;
    
    using System.Data;
    using System.Data.SqlClient;
    
    using System.Web;
    using System.Xml;
    using Telligent.DynamicConfiguration.Components;
    using Telligent.Evolution.Extensibility.Api.Version1;
    using Telligent.Evolution.Extensibility.Authentication.Version1;
    using Telligent.Evolution.Extensibility.Storage.Version1;
    using Telligent.Evolution.Extensibility.UI.Version1;
    using Telligent.Evolution.Extensibility.Urls.Version1;
    using Telligent.Evolution.Extensibility.Version1;
    using Telligent.Services.SamlAuthenticationPlugin.Components;
    using Telligent.Services.SamlAuthenticationPlugin.Extensibility.Events;
    using Telligent.Services.SamlAuthenticationPlugin.Extensibility;
    
    namespace Telligent.Services.SamlAuthenticationPlugin.Plugins.OauthLink
    {
        public class LinkSamlUsers : ISamlOAuthLinkManager
        {
    
            public bool Enabled { get { return true; } }
            string[] cats = { "SAML", "OAuth" };
            private IEventLog _eventLogApi;
            private IUsers _usersApi;
    
            public string Name { get { return "SAML Link Manager";  } }
    
            public string Description { get { return "Links a user account with SAML token";  } }
    
            public string[] Categories { get { return cats; } }
    
            public void EnsureOAuthLink(SamlTokenData samlTokenData )
            {
                var apiUser = _usersApi.Get(new UsersGetOptions { Id = samlTokenData.UserId });
                
                var oauthData = new OAuthData();
                oauthData.ClientId = apiUser.Id.ToString();
                oauthData.ClientType = samlTokenData.ClientType;
                oauthData.Email = apiUser.PrivateEmail; 
                oauthData.UserName = apiUser.Username; 
    
                if (!SqlData.isUserLinked(oauthData) && apiUser.Id.HasValue)
                {
                    // TODO: use an upgrade safe method
                    // to link saml user to account 
                    SqlData.Insert_te_OAuth_Links(samlTokenData.ClientType, samlTokenData.NameId, apiUser.Id.Value);
                }
                 
    
            }
    
    
            public void Initialize()
            {
                 _eventLogApi = PublicApi.Eventlogs;
                 _usersApi = PublicApi.Users; 
                
            }
    
           
            
        }
    }
    

    sql data to check and insert data.. 

    internal static bool isUserLinked(OAuthData oauthData)
            {
                bool _isUserLinked = false;
    
                bool clientIdParsed = Int32.TryParse(oauthData.ClientId, out int userId);
                if (clientIdParsed)
                {
    
                    try
                    {
                        using (var conn = GetSqlConnection())
                        {
    
                            var sql = string.Format(
                                  @"SELECT COUNT(*) FROM [{0}].[te_OAuth_Links] WHERE [UserId] = @userId",
                                    databaseOwner);
                            var myCommand = new SqlCommand(sql, conn); // { CommandType = CommandType.Text };
    
                            myCommand.Parameters.Add("@userId", SqlDbType.Int).Value = userId;
                            conn.Open();
                            int myCount = (Int32)myCommand.ExecuteScalar();
                            _isUserLinked = myCount > 0;
                            //myCommand.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                        PublicApi.Eventlogs.Write("Error updating from dte_OAuth_Links. " + ex.ToString(), new EventLogEntryWriteOptions() { Category = "SAML", EventId = 6010, EventType = "Error" });
                    }
                }
                return _isUserLinked;
            }
    
             
            internal static void Insert_te_OAuth_Links(string clientType, string nameId, int userId)
            {
                try
                {
    
    
                    using (var conn = GetSqlConnection())
                    {
                        var sql = string.Format(
                              @"INSERT INTO [{0}].[te_OAuth_Links]
                                ([UserId]
                                ,[ClientType]
                                ,[ClientUserId])
                            VALUES
                                (@userId
                                ,@clientType
                                ,@clientUserId )",
                                databaseOwner);
                        var myCommand = new SqlCommand(sql, conn) { CommandType = CommandType.Text };
    
                        myCommand.Parameters.Add("@userId", SqlDbType.Int).Value = userId;
                        myCommand.Parameters.Add("@ClientType", SqlDbType.Text).Value = clientType;
                        myCommand.Parameters.Add("@clientUserId", SqlDbType.NVarChar).Value = nameId;
    
                        conn.Open();
                        myCommand.ExecuteNonQuery();
    
    
    
                    }
                }
                catch (Exception ex)
                {
                    PublicApi.Eventlogs.Write("Error updating from dte_OAuth_Links. " + ex.ToString(), new EventLogEntryWriteOptions() { Category = "SAML", EventId = 6010, EventType = "Error" });
                }
    
            }

  • Direct interaction with the schema is not supported.  You should not need it at all, you should always return new OAuthData adn the platform looks it up. 

    Also this version is end of life and has been for some time, please upgrade to 12.1.

  • I know I know. It's so embarrassing.  We have our upgrade on the road map. Our hold up is that we have an old authentication system that we must retire and replace first, using saml, and then upgrade Telligent 12.x.