code: def on_identity_changed(sender, identity):
      
      if isinstance(identity, AnonymousIdentity):
          return
  
      client_id = current_app.config['CERN_APP_CREDENTIALS']['consumer_key']
      account = RemoteAccount.get(
          user_id=current_user.get_id(),
          client_id=client_id,
      )
      groups = []
      if account:
          remote = find_remote_by_client_id(client_id)
          resource = get_resource(remote)
          refresh = current_app.config.get(
              'OAUTHCLIENT_CERN_REFRESH_TIMEDELTA',
              OAUTHCLIENT_CERN_REFRESH_TIMEDELTA
          )
  
          groups.extend(
              account_groups_and_extra_data(account, resource,
                                            refresh_timedelta=refresh)
          )
  
      extend_identity(identity, groups)
docs: Store groups in session whenever identity changes.
  
      :param identity: The user identity where information are stored.