Improving API: authentication.

This commit is contained in:
Offray Vladimir Luna Cárdenas 2017-06-09 07:42:57 +00:00
parent 48968316c6
commit a259e0b500

View File

@ -2,6 +2,11 @@
I model a fossil repository. For details about fossil see: I model a fossil repository. For details about fossil see:
http://fossil-scm.org/ http://fossil-scm.org/
The protocols are named following Smalltalk conventions, but
also after the Fossil JSON API documentation at [1]
[1] https://docs.google.com/document/d/1fXViveNhDbiXgCuE7QDXQOKeFzf2qNUkBEgiUvoqFN4/view
" "
Class { Class {
#name : #FossilRepo, #name : #FossilRepo,
@ -13,24 +18,58 @@ Class {
#category : #Fossil #category : #Fossil
} }
{ #category : #authentication }
FossilRepo >> authTokenFor: anUserName withPassword: passwordString [
^ ((self loginAs: anUserName withPassword: passwordString) at: 'payload') at: 'authToken'
]
{ #category : #authentication }
FossilRepo >> capabilities [
| payload name permissions |
payload := self rawCapabilities at: 'payload'.
name := payload at: 'name'.
permissions := ((payload at: 'permissionFlags') select: [ :item | item value ]) keys.
^ Dictionary new
at: 'name' put: name;
at: 'permissions' put: permissions;
yourself.
]
{ #category : #querying } { #category : #querying }
FossilRepo >> checkinsFor: relativeFilePath [ FossilRepo >> checkinsFor: relativeFilePath [
"I get all histotical checkins information for a full file name, wich includes relative path "I get all histotical checkins information for a full file name, wich includes relative path
in the repository (i.e: 'Doc/Es/Tutoriales/tutorial.ston' or 'index.html's)" in the repository (i.e: 'Doc/Es/Tutoriales/tutorial.ston' or 'index.html's)"
(self jsonDataFor: relativeFilePath) = self ifTrue: [ (self jsonWikiDataFor: relativeFilePath) = self ifTrue: [
self inform: self inform:
'WARNING! Key not found, verify the file name you are looking in this repository'. 'WARNING! Key not found, verify the file name you are looking in this repository'.
^ self ]. ^ self ].
^ (((self jsonDataFor: relativeFilePath) at: 'payload') at: 'checkins') ^ (((self jsonWikiDataFor: relativeFilePath) at: 'payload') at: 'checkins')
] ]
{ #category : #wiki } { #category : #wiki }
FossilRepo >> createPage: pageName [
^ NeoJSONReader fromString: (self jsonWikiDataFor: 'create/', pageName)
]
{ #category : #wiki }
FossilRepo >> fetchPage: pageName [
^ NeoJSONReader fromString: (self jsonWikiDataFor: 'get/', pageName)
]
{ #category : #utility }
FossilRepo >> jsonDataFor: anUrlSegment [ FossilRepo >> jsonDataFor: anUrlSegment [
^ ZnClient new ^ ZnClient new
get: (self wikiRoot addPathSegment: anUrlSegment); get: (self jsonRoot addPathSegment: anUrlSegment);
contents. contents.
] ]
{ #category : #utility }
FossilRepo >> jsonRoot [
"I define the root of the JSON API for all repo querying and modifiying operations"
^ self remote addPathSegment: 'json'.
]
{ #category : #querying } { #category : #querying }
FossilRepo >> jsonStringFor: aFileName [ FossilRepo >> jsonStringFor: aFileName [
| queryForJSONData | | queryForJSONData |
@ -39,6 +78,13 @@ FossilRepo >> jsonStringFor: aFileName [
^ (ZnEasy get: queryForJSONData) contents. ^ (ZnEasy get: queryForJSONData) contents.
] ]
{ #category : #wiki }
FossilRepo >> jsonWikiDataFor: anUrlSegment [
^ ZnClient new
get: (self wikiRoot addPathSegment: anUrlSegment);
contents.
]
{ #category : #querying } { #category : #querying }
FossilRepo >> lastHashNumberFor: aFileName [ FossilRepo >> lastHashNumberFor: aFileName [
"I'm useful to see if local versions of files are updated to the last versions of the "I'm useful to see if local versions of files are updated to the last versions of the
@ -56,9 +102,32 @@ FossilRepo >> local: aLocalFilePath [
local := aLocalFilePath local := aLocalFilePath
] ]
{ #category : #authentication }
FossilRepo >> loginAs: anUserName withPassword: password [
| jsonData |
jsonData := ZnClient new
url: (self loginUrlWithName: anUserName andPassword: password);
get;
contents.
^ NeoJSONReader fromString: jsonData
]
{ #category : #authentication }
FossilRepo >> loginUrlWithName: aUser andPassword: passwd [
^ self jsonRoot
addPathSegment: 'login';
queryAt: 'name' put: aUser;
queryAt: 'password' put: passwd.
]
{ #category : #wiki } { #category : #wiki }
FossilRepo >> pageList [ FossilRepo >> pageList [
^ NeoJSONReader fromString: (self jsonDataFor: 'list') ^ NeoJSONReader fromString: (self jsonWikiDataFor: 'list')
]
{ #category : #authentication }
FossilRepo >> rawCapabilities [
^ NeoJSONReader fromString: (self jsonDataFor: 'cap')
] ]
{ #category : #accessing } { #category : #accessing }
@ -71,6 +140,11 @@ FossilRepo >> remote: anUrlString [
remote := anUrlString asUrl remote := anUrlString asUrl
] ]
{ #category : #authentication }
FossilRepo >> whoAmI [
^ NeoJSONReader fromString: (self jsonDataFor: 'whoami')
]
{ #category : #wiki } { #category : #wiki }
FossilRepo >> wikiRoot [ FossilRepo >> wikiRoot [
^ self jsonRoot addPathSegment: 'wiki' ^ self jsonRoot addPathSegment: 'wiki'
@ -78,5 +152,5 @@ FossilRepo >> wikiRoot [
{ #category : #wiki } { #category : #wiki }
FossilRepo >> wikiTimeline [ FossilRepo >> wikiTimeline [
^ NeoJSONReader fromString: (self jsonDataFor: 'timeline') ^ NeoJSONReader fromString: (self jsonWikiDataFor: 'timeline')
] ]