@@ -125,6 +125,7 @@ def __init__(self,
125125 neighborhood = None ,
126126 cached_neighborhood = None ,
127127 complete = True ,
128+ is_new = False ,
128129 ):
129130 """Store basic information about a patron.
130131
@@ -203,6 +204,8 @@ def __init__(self,
203204 complete data we are likely to get for this patron from this
204205 data source, or is it an abbreviated version of more complete
205206 data we could get some other way?
207+
208+ :param is_new: Is this the first time we are seeing this Patron?
206209 """
207210 self .permanent_id = permanent_id
208211
@@ -214,6 +217,7 @@ def __init__(self,
214217 self .block_reason = block_reason
215218 self .library_identifier = library_identifier
216219 self .complete = complete
220+ self .is_new = is_new
217221
218222 # We do not store personal_name in the database, but we provide
219223 # it to the client if possible.
@@ -442,7 +446,8 @@ def scrub(value, default=None):
442446 external_type = self .external_type ,
443447 block_reason = self .block_reason ,
444448 personal_name = self .personal_name ,
445- email_address = self .email_address
449+ email_address = self .email_address ,
450+ is_new = self .is_new ,
446451 )
447452 data = dict ((k , scrub (v )) for k , v in list (data .items ()))
448453
@@ -1787,6 +1792,8 @@ class BasicAuthenticationProvider(AuthenticationProvider, HasSelfTests):
17871792 # indicates that no value should be used.)
17881793 class_default = object ()
17891794
1795+ patron_is_new = False
1796+
17901797 def __init__ (self , library , integration , analytics = None ):
17911798 """Create a BasicAuthenticationProvider.
17921799
@@ -1851,6 +1858,8 @@ def __init__(self, library, integration, analytics=None):
18511858 _db , self .HTTP_BASIC_OAUTH_ENABLED , library , integration
18521859 ).bool_value or self .HTTP_BASIC_OAUTH_ENABLED_DEFAULT
18531860
1861+ self .patron_is_new = False
1862+
18541863 def remote_patron_lookup (self , patron_or_patrondata ):
18551864 """Ask the remote for information about this patron, and then make sure
18561865 the patron belongs to the library associated with thie BasicAuthenticationProvider."""
@@ -2023,6 +2032,7 @@ def _authenticate_from_credentials(self, _db, credentials):
20232032 # Just make sure our local data is up to date with
20242033 # whatever we just got from remote.
20252034 self .apply_patrondata (patrondata , patron )
2035+ self .patron_is_new = False
20262036 return patron
20272037
20282038 # At this point there are two possibilities:
@@ -2051,6 +2061,7 @@ def _authenticate_from_credentials(self, _db, credentials):
20512061 # For whatever reason, the remote lookup implementation
20522062 # returned a Patron object instead of a PatronData. Just
20532063 # use that Patron object.
2064+ self .patron_is_new = False
20542065 return patrondata
20552066
20562067 # At this point we have a _complete_ PatronData object which we
@@ -2064,6 +2075,9 @@ def _authenticate_from_credentials(self, _db, credentials):
20642075 patron , is_new = patrondata .get_or_create_patron (
20652076 _db , self .library_id , analytics = self .analytics
20662077 )
2078+ self .patron_is_new = is_new
2079+ else :
2080+ self .patron_is_new = False
20672081
20682082 # The lookup failed in the first place either because the
20692083 # Patron did not exist on the local side, or because one of
@@ -2506,6 +2520,7 @@ def oauth_callback(self, _db, code):
25062520 patron , is_new = patrondata .get_or_create_patron (
25072521 _db , self .library_id , analytics = self .analytics
25082522 )
2523+ patrondata .is_new = is_new
25092524
25102525 # Create a credential for the Patron.
25112526 credential , is_new = self .create_token (_db , patron , token )
@@ -2687,6 +2702,7 @@ def oauth_authentication_callback(self, _db, params):
26872702 access_token = simplified_token ,
26882703 patron_info = patron_info ,
26892704 root_lane = root_lane ,
2705+ is_new = patrondata .is_new ,
26902706 )
26912707 return redirect (client_redirect_uri + "#" + urllib .parse .urlencode (params ))
26922708
@@ -2775,6 +2791,11 @@ def get_or_create_token(self, _db, patron):
27752791 def basic_auth_temp_token (self , params , _db ):
27762792 """Generate and return a temporary token from HTTP Basic Auth credentials.
27772793 """
2794+ short_name = self .authenticator .current_library_short_name
2795+ providers = self .authenticator .library_authenticators [short_name ].providers
2796+ basic_auth_providers = list (filter (lambda provider : isinstance (provider , BasicAuthenticationProvider ), providers ))
2797+ is_new = any ([provider .patron_is_new for provider in basic_auth_providers ])
2798+
27782799 patron = self .authenticator .authenticated_patron (
27792800 _db , flask .request .authorization )
27802801
@@ -2806,6 +2827,7 @@ def basic_auth_temp_token(self, params, _db):
28062827 token_type = "bearer" ,
28072828 expires_in = BasicAuthTempTokenController .TOKEN_DURATION .seconds ,
28082829 root_lane = root_lane ,
2830+ is_new = is_new ,
28092831 )
28102832
28112833 return flask .jsonify (data )
0 commit comments