Search code examples
ruby-on-railsrubyyammer

Returning the network subdomain from Yammer


I have a ruby on rails application that authenticates the users through Yammer and then redirects them to the right tenant depending on their network name.

Workflow essentially goes like this:

  1. User is presented with sign-in/sign-up page

  2. Authenticate through the Yammer API and redirected back to a callback URL (yammer.example.com/auth)

  3. The callback controller then looks at the auth response, and determines the network that the user belongs to.

  4. I redirect to that tenant on the subdomain (eg my-network.example.com) and sign the user in

There are some other things that go on here in the background (creation of other users in the network, user matching etc.) but the problem I am having is with the actual network name and subdomain creation and redirection.

The way that the subdomain is currently parsed is to use the SLD of the users network name.

As an example network_name: "example.com" returns "example" as the subdomain for us to create/redirect to.

This was all working great until we started testing with paid Yammer accounts, their network names seem to not play nice with our current code.

This is what we are currently using:

if PublicSuffix.valid?(auth.extra.raw_info.network_name.mb_chars.normalize(:kc).to_s.downcase)
  yammer_domain = PublicSuffix.parse(auth.extra.raw_info.network_name.mb_chars.normalize(:kc).downcase)
  subdomain = yammer_domain.sld.gsub(/[^0-9A-Za-z]/, '')
else
  subdomain = auth.extra.raw_info.network_name.downcase.gsub(/[^0-9A-Za-z]/, '')
end

I'll admit that this is not the cleanest at the moment because I have been hacking a little trying to catch when a network name is not a correct domain and fixing it there. I am normailizing all characters and then parsing the SLD using the PublicSuffix gem.

If it is not a valid domain then I try and normalize the characters and strip everything out that we don't need (so something like L'oreal would just become loreal).

This still seems to throw an error and not parse correctly.

So my question is:

Is there anything different about how the network names are set up with paid accounts vs. free accounts? Is there a more reliable way to return the network name to parse for subdomains using the Yammer API?


Solution

  • Is there anything different about how the network names are set up with paid accounts vs. free accounts?

    No there isn't.

    Is there a more reliable way to return the network name to parse for subdomains using the Yammer API?

    If I understand what you're trying to do correctly, I don't think "network_name" is the correct JSON object to use. The network admin can decide to change the network name any time and that would screw your app.

    I'd recommend you use "network_domains". The value of this JSON object contains a list of all yammer networks the logged-in user is a member of, and the first item in the list is ALWAYS the primary network of the user. For example, the result of a GET request to api/v1/users/current.json would contain something like:

    network_domains":["jet.usa.cc","jet.onmicrosoft.com","jet2.onmicrosoft.com"]

    In the above example, jet.usa.cc is the primary network of the logged-in user. The network domain name cannot be changed, it's a constant. You may extract the value of the primary network in your RoR app and use it as you wish.