Module: Platforms::Yammer::Authentication
- Extended by:
- ActiveSupport::Concern
- Includes:
- Core::OAuth2
- Defined in:
- lib/platforms/yammer/authentication.rb
Overview
A module for Controllers to include so that the OAuth2 flow can be correctly captured from OmniAuth.
The #save_identity method will set instance variables in the Controller for:
-
@token, which should be saved in the session for making subsequent API requests.
-
@platforms_user, which identifies the authenticated User
-
@platforms_network, the Network that the authenticated user belongs to
-
@switch_network_ids, an Array of Network ids that the user can switch to (subject to API permissions).
Instance Method Summary collapse
-
#client ⇒ Platforms::Yammer::Client
The Faraday-based client to make REST requests.
-
#save_identity(groups = true) ⇒ Platforms::User
Save the current identity.
-
#set_token ⇒ String
Set the token within the class.
-
#switch_identity(client, permalink, groups = true) ⇒ Platforms::User
Switch the current identity.
Instance Method Details
#client ⇒ Platforms::Yammer::Client
The Faraday-based client to make REST requests
This is required for #save_identity to get additional information about the network context.
69 70 71 |
# File 'lib/platforms/yammer/authentication.rb', line 69 def client @client ||= Client.new @token end |
#save_identity(groups = true) ⇒ Platforms::User
Save the current identity
Note that OmniAuth::Yammer effectively delivers /users/current.json as the extra.raw_info. Rather than delivering the original /oauth2/access_token response. Therefore we need to make another call to /networks/current.json to get the list of (known) networks.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/platforms/yammer/authentication.rb', line 82 def save_identity groups=true omni = request.env["omniauth.auth"] raw_info = omni.extra.raw_info # Get the network information, conveniently for all known networks # (should return an array) networks_response = client.networks.current current_network = nil networks = networks_response.body @switch_network_ids ||= [] # Create a Platforms::Network, or update it with current details # if it already exists networks.each do |network| n = Platforms::Network.find_or_initialize_by( platform_id: network["id"] ) n.name = network["name"] n.trial = bool_safe network["is_freemium"] n.permalink = network["permalink"] n.platform_type = "yammer" n.save! @switch_network_ids << n.id # Set @platform_network to the filtered network @platforms_network = n if raw_info.network_id.to_s == n.platform_id end user = Platforms::User.find_or_initialize_by( platform_id: omni.uid, platforms_network: @platforms_network ) user.name = omni.info.name user.thumbnail_url = omni.info.image user.admin = bool_safe raw_info.admin user.web_url = omni.info.urls.yammer user.email = omni.info.email user.save! @platforms_user = user # Switch for including groups if groups current_user = client.users. current({include_group_memberships: true}) sync_groups(current_user.body) end # Return @platforms_user @platforms_user end |
#set_token ⇒ String
Set the token within the class
A convenience method, as the token is available in request.env
60 61 62 |
# File 'lib/platforms/yammer/authentication.rb', line 60 def set_token @token ||= request.env["omniauth.auth"].credentials.token end |
#switch_identity(client, permalink, groups = true) ⇒ Platforms::User
Switch the current identity
This does much the same as #save_identity, but the inputs are very different. This does not use the OmniAuth flow, but instead makes a call to the authenticated API to get the required information.
switching.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/platforms/yammer/authentication.rb', line 147 def switch_identity client, permalink, groups=true # Do not use @client as that uses the token from OmniAuth. # switch_identity is designed to be used later in the user flow. tokens = client.oauth.tokens.body switch_network = tokens.find{ |t| t["network_permalink"] == permalink } # Raise error if the token is not found if switch_network.nil? raise ActiveRecord::RecordNotFound.new "Couldn't find Platforms::Network for permalink #{permalink}" end # Create a new Client with the updated token @token = switch_network["token"] switch_client = Platforms::Yammer::Client.new @token # Don't worry about updating Network here, should be done at first login. @platforms_network = Platforms::Network.find_by!( permalink: permalink, platform_type: "yammer" ) # Switch for including groups = groups ? { include_group_memberships: true } : {} switch_user = switch_client.users.current().body user = Platforms::User.find_or_initialize_by( platform_id: switch_network["user_id"], platforms_network: @platforms_network ) user.name = switch_user["full_name"] user.thumbnail_url = switch_user["mugshot_url"] user.admin = bool_safe switch_user["admin"] user.web_url = switch_user["web_url"] user.email = switch_user["email"] user.save! @platforms_user = user sync_groups(switch_user) if groups # Return @platforms_user @platforms_user end |