Ejemplo n.º 1
0
    def testadd_auth_params(self):
        my_user = user.User(
            principal_id='8eaa9f92-3951-431c-975a-e5dt26b7d232',
            principal_idns='urn:oclc:wms:ad',
            authenticating_institution_id='128807')

        auth_params = {'userid': 'tasty', 'password': '******'}
        """ Both User and Auth params exists """
        self.assertEqual(
            self._my_wskey.add_auth_params(user=my_user,
                                           auth_params=auth_params),
            ('password="******",' +
             'principalID="8eaa9f92-3951-431c-975a-e5dt26b7d232",' +
             'principalIDNS="urn:oclc:wms:ad",' + 'userid="tasty"'))
        """ Just User params """
        self.assertEqual(
            self._my_wskey.add_auth_params(user=my_user, auth_params=None),
            'principalID="8eaa9f92-3951-431c-975a-e5dt26b7d232",principalIDNS="urn:oclc:wms:ad"'
        )
        """ Just Auth params """
        self.assertEqual(
            self._my_wskey.add_auth_params(user=None, auth_params=auth_params),
            'password="******",userid="tasty"')
        """ Neither User nor Auth params exist."""
        self.assertEqual(
            self._my_wskey.add_auth_params(user=None, auth_params=None), '')
Ejemplo n.º 2
0
def sendPutRequestToOCLC(url, bodyData):
    """ Function builds a put request to OCLC's API and send data
    """

    configData = parseConfigFile("config.json")

    #Assign variables
    key = configData["client_id"]
    secret = configData["client_secret"]
    principal_id = configData["principalID"]
    principal_idns = configData["principalIDNS"]
    institution_id = configData["institutionId"]

    my_wskey = wskey.Wskey(key=key, secret=secret, options=None)

    my_user = user.User(authenticating_institution_id=institution_id,
                        principal_id=principal_id,
                        principal_idns=principal_idns)

    authorization_header = my_wskey.get_hmac_signature(method='PUT',
                                                       request_url=url,
                                                       options={
                                                           'user': my_user,
                                                           'auth_params': None
                                                       })

    my_request = requests.put(url=url,
                              data=bodyData,
                              headers={
                                  'Authorization': authorization_header,
                                  "accept": "application/atom+json",
                                  "Content-Type": "application/atom+json"
                              })

    return my_request
Ejemplo n.º 3
0
    def testget_hmac_signature(self):
        self._my_wskey.debug_time_stamp = '1392239490'
        self._my_wskey.debug_nonce = '0x16577027'

        AuthenticationHeader = self._my_wskey.get_hmac_signature(
            method='GET',
            request_url=('https://worldcat.org/bib/data/1039085' +
                         '?inst=128807' +
                         '&classificationScheme=LibraryOfCongress' +
                         '&holdingLibraryCode=MAIN'),
            options={
                'user':
                user.User(principal_id='8eaa9f92-3951-431c-975a-e5dt26b7d232',
                          principal_idns='urn:oclc:wms:ad',
                          authenticating_institution_id='128807'),
                'auth_params': {
                    'userid': 'tasty',
                    'password': '******'
                }
            })

        expected = (
            'http://www.worldcat.org/wskey/v2/hmac/v1 ' +
            'clientID="CancdeDMjFO9vnzkDrB6WJg1UnyTnkn8lLupLKygr0U1KJLiaAittuVjGRywCDdrsxahv2sbjgKq6hLM",'
            + 'timestamp="1392239490",' + 'nonce="0x16577027",' +
            'signature="+RFPwih61799mpNBJqGhhSbQgd/JRfEinYv81z+CwRY=",' +
            'password="******",' +
            'principalID="8eaa9f92-3951-431c-975a-e5dt26b7d232",' +
            'principalIDNS="urn:oclc:wms:ad",' + 'userid="tasty"')

        self.assertEqual(AuthenticationHeader, expected)
Ejemplo n.º 4
0
    def __init__(self, params, options=None):
        self.key = params.get('key')
        self.secret = params.get('secret')
        self.principleId = params.get('principleId')
        self.principleIDNS = params.get('principleIDNS')
        self.institutionId = params.get('institutionId')
        self.options = options
        self.etag = None

        self.user = user.User(authenticating_institution_id=self.institutionId,
                              principal_id=self.principleId,
                              principal_idns=self.principleIDNS)

        self.wsKey = wskey.Wskey(key=self.key,
                                 secret=self.secret,
                                 options=self.options)
Ejemplo n.º 5
0
    def testParseTokenResponse(self):
        sample_access_token = accesstoken.AccessToken(
            self._authorization_server, 'authorization_code', self._options)
        sample_access_token.parse_token_response(
            '{' + '"expires_at":"2014-03-13 15:44:59Z",' +
            '"principalIDNS":"urn:oclc:platform:128807",' +
            '"principalID":"2334dd24-b27e-49bd-8fea-7cc8de670f8d",' +
            '"error_code":"trouble",' + '"expires_in":1199,' +
            '"token_type":"bearer",' + '"context_institution_id":"128807",' +
            '"access_token":"tk_25fXauhJC09E5kwFxcf4TRXkTnaRYWHgJA0W",' +
            '"refresh_token":"rt_25fXauhJC09E5kwFxcf4TRXkTnaRYWHgJA0W",' +
            '"refresh_token_expires_in":1900,' +
            '"refresh_token_expires_at":"2014-03-13 15:44:59Z"' + '}')
        expected_user = user.User(
            authenticating_institution_id='128807',
            principal_id='2334dd24-b27e-49bd-8fea-7cc8de670f8d',
            principal_idns='urn:oclc:platform:128807')

        expected_refresh_token = refreshtoken.RefreshToken(
            tokenValue='rt_25fXauhJC09E5kwFxcf4TRXkTnaRYWHgJA0W',
            expires_in=1900,
            expires_at='2014-03-13 15:44:59Z')

        self.assertEqual(sample_access_token.access_token_string,
                         'tk_25fXauhJC09E5kwFxcf4TRXkTnaRYWHgJA0W')
        self.assertEqual(sample_access_token.type, 'bearer')
        self.assertEqual(sample_access_token.expires_at,
                         '2014-03-13 15:44:59Z')
        self.assertEqual(sample_access_token.expires_in, 1199)
        self.assertEqual(sample_access_token.error_code, 'trouble')
        self.assertEqual(sample_access_token.context_institution_id, '128807')
        self.assertEqual(user.User, type(sample_access_token.user))
        self.assertEqual(
            expected_user.authenticating_institution_id,
            sample_access_token.user.authenticating_institution_id)
        self.assertEqual(expected_user.principal_id,
                         sample_access_token.user.principal_id)
        self.assertEqual(expected_user.principal_idns,
                         sample_access_token.user.principal_idns)
        self.assertEqual(refreshtoken.RefreshToken,
                         type(sample_access_token.refresh_token))
        self.assertEqual(expected_refresh_token.refresh_token,
                         sample_access_token.refresh_token.refresh_token)
        self.assertEqual(expected_refresh_token.expires_in,
                         sample_access_token.refresh_token.expires_in)
        self.assertEqual(expected_refresh_token.expires_at,
                         sample_access_token.refresh_token.expires_at)
Ejemplo n.º 6
0
    def testAccessTokenFormatterWithUser(self):
        self._access_token.user = user.User(
            authenticating_institution_id='128807',
            principal_id='2334ed24-b27e-63bd-8fea-7cw2deq70r8d',
            principal_idns='urn:oclc:platform:128807')

        self.assertEqual(
            self._my_access_token_formatter.format(),
            '<h2>Access Token</h2>' + '<table class="pure-table">' +
            '<tr><td>access_token</td><td>tk_TBHrsDbSrWW1oS7d3gZr7NJb7PokyOFlf0pr</td></tr>'
            + '<tr><td>token_type</td><td>bearer</td></tr>' +
            '<tr><td>expires_at</td><td>2014-04-08 13:38:29Z</td></tr>' +
            '<tr><td>expires_in</td><td>1198</td></tr>' +
            '<tr><td>principalID</td><td>2334ed24-b27e-63bd-8fea-7cw2deq70r8d</td></tr>'
            +
            '<tr><td>principalIDNS</td><td>urn:oclc:platform:128807</td></tr>'
            + '<tr><td>contextInstitutionId</td><td>128808</td></tr>' +
            '<tr><td>refresh_token</td><td>rt_25fXauhJC09E4kwFxcf4TREkTnaRYWHgJA0W</td></tr>'
            +
            '<tr><td>refresh_token_expires_at</td><td>2014-03-13 15:44:59Z</td></tr>'
            + '<tr><td>refresh_token_expires_in</td><td>1199</td></tr>' +
            '</table>')
Ejemplo n.º 7
0
 def testCreateUserExceptions(self):
     with self.assertRaises(user.InvalidParameter):
         user.User()
     with self.assertRaises(user.InvalidParameter):
         user.User(**{
             'principal_idns': 'urn:oclc:wms:da',
             'authenticating_institution_id': '128807'
         })
     with self.assertRaises(user.InvalidParameter):
         user.User(**{
             'principal_id': '8eaa9f92-3951-431c-975a-e5dt26b7d232',
             'authenticating_institution_id': '128807'
         })
     with self.assertRaises(user.InvalidParameter):
         user.User(**{
             'principal_id': '8eaa9f92-3951-431c-975a-e5dt26b7d232',
             'principal_idns': 'urn:oclc:wms:da'
         })
     with self.assertRaises(user.InvalidParameter):
         user.User(**{
             'principal_id': '',
             'principal_idns': 'urn:oclc:wms:da',
             'authenticating_institution_id': '128807'
         })
     with self.assertRaises(user.InvalidParameter):
         user.User(**{
             'principal_id': '8eaa9f92-3951-431c-975a-e5dt26b7d232',
             'principal_idns': '',
             'authenticating_institution_id': '128807'
         })
     with self.assertRaises(user.InvalidParameter):
         user.User(**{
             'principal_id': '8eaa9f92-3951-431c-975a-e5dt26b7d232',
             'principal_idns': 'urn:oclc:wms:da',
             'authenticating_institution_id': ''
         })
Ejemplo n.º 8
0
def perpetualAccessCheck(collUid):
	global perAccess
	global archivalCopy
	perAccess = ''
	archivalCopy = ''
#authentication code for License Manager API taken from OCLC documentation: https://github.com/OCLC-Developer-Network/oclc-auth-python
	request_url = 'https://1284.share.worldcat.org/license-manager/license/search?q=collectionId:' + collUid
	my_wskey = wskey.Wskey(
		key=lmKey,
		secret=secret,
		options=None
		)

	my_user = user.User(
		authenticating_institution_id=authenticating_institution_id,
		principal_id=principal_id,
		principal_idns=principal_idns
		)

	authorization_header = my_wskey.get_hmac_signature(
		method='GET',
		request_url=request_url,
		options={
		'user': my_user,
		'auth_params': None}
		)
	print (request_url)
	myRequest = urllib2.Request(
		url=request_url,
		data=None,
		headers={'Authorization': authorization_header}
		)
	time.sleep(1)
	r = urllib2.urlopen(myRequest).read()
	root = ET.fromstring(r)
	resultCheck = root.find('os:totalResults', ns)
	emptyCheck = resultCheck.text
	print('Results: ' + emptyCheck)
	if emptyCheck == "0":
		print('no license found')
		perAccess = 'no license found'
		archivalCopy = 'no license found'
		
	else:
		customTermPath = root.findall("atom:entry/atom:content/df:license/df:terms/df:term/[df:type='Custom Term']", ns)
		for match in customTermPath:
			namePath = match.findall("./df:name", ns)
			termValuePath = match.findall("./df:termValue", ns)
			for name in namePath:
				for termValue in termValuePath:
					if name.text == 'Perpetual Access Rights':
						if termValue.text == 'yes':
							perAccess = 'yes'
					else:
						perAccess = 'no or silent'
						#elif termValue.text == 'no':
						#	perAccess = 'no'
						#elif termValue.text == 'silent':
						#	perAccess = 'silent'
					#	elif termValue.txt is None:
						#	perAccess = 'silent'
		archivalCopyValue = root.find("atom:entry/atom:content/df:license/df:terms/df:term/[df:type='Archival_Copy_Provided']/df:termValue", ns)
		if archivalCopyValue is None:
			archivalCopy = 'silent'
		elif archivalCopyValue.text == 'yes':
			archivalCopy = 'yes'
		elif archivalCopyValue.text == 'no':
			archivalCopy = 'no'
		else:
			archivalCopy = 'silent'
Ejemplo n.º 9
0
        print "Response Code = %i" % e.code
        print response_body
        if config.key == '{clientID}':
            print('\n** Note: Edit the script and supply \
                  valid authentication parameters. **\n')

    return bibresult


if __name__ == "__main__":
    import config

    my_wskey = wskey.Wskey(key=config.key, secret=config.secret, options=None)

    my_user = user.User(
        authenticating_institution_id=config.authenticating_institution_id,
        principal_id=config.principal_id,
        principal_idns=config.principal_idns)

    idfile = open(config.idfile, 'r')
    outfile = codecs.open(config.outfile, 'w', 'utf-8')
    outfile.write('OCLC ID,SUDOC,TITLE,YEAR,FORMAT\n')
    lastid = "0"
    lastbibinfo = {}
    for line in idfile.readlines():
        if len(line.strip()) == 0:
            outfile.write(",,,,\n")
        else:
            oclc_id = line[:-1]
            if oclc_id == lastid:
                # Don't re-look up the same id, but we still want a line
                bibinfo = lastbibinfo
Ejemplo n.º 10
0
 def setUp(self):
     self._user = user.User(
         principal_id= '8eaa9f92-3951-431c-975a-e5dt26b7d232',
         principal_idns= 'urn:oclc:wms:da',
         authenticating_institution_id= '128807'
     )
Ejemplo n.º 11
0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################

# Sample HMAC Hashing for any sandbox OCLC record retrieval

from authliboclc import wskey, user

#
# You must supply these parameters to authenticate
# Note - a WSKey consists of two parts, a public clientID and a private secret
#

key = {key}
secret = {secret}
principal_id = {id}
principal_idns = {idns}
authenticating_institution_id = {inst_id}

my_wskey = wskey.Wskey(key=key, secret=secret, options=None)

my_user = user.User(
    authenticating_institution_id=authenticating_institution_id,
    principal_id=principal_id,
    principal_idns=principal_idns)
Ejemplo n.º 12
0
def setUser():
    my_user = user.User(
        authenticating_institution_id=authenticating_institution_id,
        principal_id=principal_id,
        principal_idns=principal_idns)
    return my_user