Skip to content

Commit

Permalink
refactor authentication. Detect garbage token or token with not suffi…
Browse files Browse the repository at this point in the history
…cient scopes
  • Loading branch information
Matthias Cram committed Aug 2, 2022
1 parent 26b0438 commit 07f17df
Show file tree
Hide file tree
Showing 66 changed files with 251 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildButtons: builder

^ {builder pluggableButtonSpec new
model: self;
label: 'Change';
label: 'Submit';
action: #actionSaveAndClose;
yourself.
builder pluggableButtonSpec new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"actionToken:" : "lo 7/13/2022 19:25",
"actionUsername" : "lo 7/13/2022 19:21",
"actionUsername:" : "lo 7/13/2022 19:25",
"buildButtons:" : "lo 8/1/2022 14:18",
"buildButtons:" : "mcr 8/2/2022 16:48",
"buildInputFields:" : "lo 8/1/2022 14:19",
"buildInputPanel:" : "lo 8/1/2022 14:19",
"buildWith:" : "lo 8/1/2022 10:51",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"instvars" : [
"token",
"username" ],
"name" : "SPBUserAuth",
"name" : "SPBAuthenticationForm",
"pools" : [
],
"super" : "Model",
Expand Down
7 changes: 7 additions & 0 deletions Squello-Core.package/SPBAuthenticator.class/class/newWith..st
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instance creation
newWith: anSPBGithubBoardProvider

| instance |
instance := self new.
instance boardProvider: anSPBGithubBoardProvider.
^ instance.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
authentication
authenticate

self checkIfCredentialsMissing
ifTrue: [SPBAuthenticationForm open].

self checkIfCredentialsMissing
ifTrue: [self errorAuthenticationCanceled. Error signal].

[self checkIfTokenValidFor]
on: Error do: [Error signal].
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
boardProvider: anSPBGithubBoardProvider

boardProvider := anSPBGithubBoardProvider.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
boardProvider

^ boardProvider.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
authentication
checkIfCredentialsMissing

self username: SPBGithubAPI username.
self token: SPBGithubAPI token.

^ username isNil or: [token isNil].
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
authentication
checkIfTokenValidFor

| tokenScopes repo |
[tokenScopes := self getTokenScopes]
on: Error do: [Error signal].

"test if you can query the repo with this token"
repo := self boardProvider getRepo.
repo message = 'Not Found'
ifTrue:
[(tokenScopes includes: 'repo')
ifTrue: [self errorRepoNotFound].
(tokenScopes includes: 'public_repo')
ifTrue: [self errorInvalidTokenScopes]].

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
errors
errorAuthenticationCanceled

UserDialogBoxMorph
inform: 'Authentication dialog was dismissed' title: 'Operation canceled'.
^ Error signal.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
errors
errorGarbageToken

UserDialogBoxMorph
inform: 'Not a valid token' title: 'Operation canceled'.
SPBGithubAPI token: nil.
^ Error signal.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
errors
errorInvalidTokenScopes

UserDialogBoxMorph
inform: 'Invalid token or username or repository is private. Token has only public_repo scope' title: 'Operation canceled'.
SPBGithubAPI token: nil.
^ Error signal.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
errors
errorNoScopes

UserDialogBoxMorph
inform: 'Make sure the token has repo or public_repo scope' title: 'Operation cancelled'.
SPBGithubAPI token: nil.
^ Error signal.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
errors
errorRepoNotFound

UserDialogBoxMorph
inform: 'Make sure you have access to the repository' title: 'Repository not found'.
^ Error signal.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
authentication
getRepoFromUrl: aString

| pathComponents url |
url := aString asUrl.
pathComponents := url fullPath splitBy: '/'.
self boardProvider user: (pathComponents at: 2).
self boardProvider repo: (pathComponents at: 3).
^ self boardProvider getRepo.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
authentication
getTokenScopes

| tokenScopes |
tokenScopes := self boardProvider getTokenOAuthScopes.
tokenScopes isEmpty
ifTrue: [self errorGarbageToken].

(((tokenScopes includes: 'repo') not) and: [(tokenScopes includes: 'public_repo') not])
ifTrue: [self errorNoScopes].
^ tokenScopes.


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
token: aString

token := aString.
4 changes: 4 additions & 0 deletions Squello-Core.package/SPBAuthenticator.class/instance/token.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
token

^ token.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
username: aString

username := aString.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
username

^ username.
20 changes: 20 additions & 0 deletions Squello-Core.package/SPBAuthenticator.class/methodProperties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"class" : {
"newWith:" : "mcr 8/2/2022 17:38" },
"instance" : {
"authenticate" : "mcr 8/2/2022 22:48",
"boardProvider" : "mcr 8/2/2022 18:26",
"boardProvider:" : "mcr 8/2/2022 18:26",
"checkIfCredentialsMissing" : "mcr 8/2/2022 18:27",
"checkIfTokenValidFor" : "mcr 8/2/2022 22:48",
"errorAuthenticationCanceled" : "mcr 8/2/2022 21:49",
"errorGarbageToken" : "mcr 8/2/2022 22:08",
"errorInvalidTokenScopes" : "mcr 8/2/2022 22:54",
"errorNoScopes" : "mcr 8/2/2022 22:31",
"errorRepoNotFound" : "mcr 8/2/2022 21:50",
"getRepoFromUrl:" : "mcr 8/2/2022 20:47",
"getTokenScopes" : "mcr 8/2/2022 22:38",
"token" : "mcr 8/2/2022 18:26",
"token:" : "mcr 8/2/2022 18:26",
"username" : "mcr 8/2/2022 18:27",
"username:" : "mcr 8/2/2022 18:27" } }
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
],
"commentStamp" : "",
"instvars" : [
],
"name" : "SquelloCoreServiceProvider",
"boardProvider",
"token",
"username" ],
"name" : "SPBAuthenticator",
"pools" : [
],
"super" : "ServiceProvider",
"super" : "Object",
"type" : "normal" }
5 changes: 5 additions & 0 deletions Squello-Core.package/SPBBoard.class/class/errorParsingUrl.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
startup
errorParsingUrl

UserDialogBoxMorph
inform: 'Invalid repository Url' title: 'Error parsing Url'.
24 changes: 12 additions & 12 deletions Squello-Core.package/SPBBoard.class/class/newWith..st
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
startup
newWith: aString

| instance username token |
| instance authenticator |
instance := self new.
username := SPBGithubAPI username.
token := SPBGithubAPI token.

(username isNil or: [token isNil]) ifTrue: [SPBUserAuth open].
(username isNil or: [token isNil]) ifTrue: [^ UserDialogBoxMorph inform: 'Authentication dialog was dismissed' title: 'Operation canceled'].

(aString beginsWith: 'https://github.com/') ifFalse:
[^ UserDialogBoxMorph inform: 'Invalid repository url' asString title: 'Operation canceled'].

instance
loadProject: aString;
buildAndOpen.
[instance parseRepoFromUrl: aString]
on: Error do: [self errorParsingUrl. ^ self].

authenticator := SPBAuthenticator newWith: instance boardProvider.
[authenticator authenticate]
on: Error do: [^ self].

[instance loadProject: aString]
on: Error do: [^ self].

instance buildAndOpen.
^ instance.
2 changes: 1 addition & 1 deletion Squello-Core.package/SPBBoard.class/class/open.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ startup
open

| prompt |
prompt := FillInTheBlank request: 'Please enter GitHub URL of your project board:' initialAnswer: (self lastProject ifNotNil: [self lastProject] ifNil: ['']) onCancelReturn: ''.
prompt := FillInTheBlank request: 'Please enter GitHub URL of your project board or your repository:' initialAnswer: (self lastProject ifNotNil: [self lastProject] ifNil: ['']) onCancelReturn: ''.
prompt = '' ifTrue: [^ self].
self lastProject: prompt.
^self newWith: prompt.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
toolbuilder
initialize-release
errorInvalidUrl

UserDialogBoxMorph inform: 'Invalid repository url' title: 'Operation canceled'.

UserDialogBoxMorph
inform: 'Invalid repository Url or project does not exist' title: 'Operation canceled'.
^ Error signal.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
initialize-release
errorNoProjects

UserDialogBoxMorph
inform: 'Repository has no projects' title: 'Operation canceled'.
^ Error signal.
3 changes: 2 additions & 1 deletion Squello-Core.package/SPBBoard.class/instance/loadProject..st
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ loadProject: aString
(aString includesSubstring: '/projects')
ifTrue: [[self boardProvider parseInputBoardUrl: aString]
on: Error
do: [^ self errorInvalidUrl]]
do: [^ self errorInvalidUrl.]]
ifFalse: [[projects := (self boardProvider getProjects: aString)]
on: Error
do: [^ self errorInvalidUrl].
projects isEmpty ifTrue: [self errorNoProjects].
projectNames := projects
collect: [:project | project at: 'name'].
chosenProject := UIManager default
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
initialize-release
parseRepoFromUrl: aString

| pathComponents url |
url := aString asUrl.
pathComponents := url fullPath splitBy: '/'.
self boardProvider user: (pathComponents at: 2).
self boardProvider repo: (pathComponents at: 3).
^ self boardProvider getRepo.
11 changes: 7 additions & 4 deletions Squello-Core.package/SPBBoard.class/methodProperties.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"class" : {
"columnMinimumExtent" : "lo 8/1/2022 13:40",
"errorParsingUrl" : "mcr 8/2/2022 22:23",
"lastProject" : "tk 7/30/2022 21:54",
"lastProject:" : "tk 7/30/2022 21:54",
"newWith:" : "NTK 7/30/2022 11:50",
"open" : "NTK 6/1/2022 16:41",
"newWith:" : "mcr 8/2/2022 22:48",
"open" : "mcr 8/2/2022 22:32",
"registerInAppsMenu" : "lo 6/4/2022 23:19" },
"instance" : {
"activeCard" : "mcr 8/1/2022 02:31",
Expand Down Expand Up @@ -44,9 +45,11 @@
"createColumns" : "lo 8/1/2022 11:11",
"createSidebar" : "lo 8/1/2022 11:11",
"errorCannotAddColumn:" : "jh 7/31/2022 14:28",
"errorInvalidUrl" : "jh 7/31/2022 14:28",
"errorInvalidUrl" : "mcr 8/2/2022 22:17",
"errorNoProjects" : "mcr 8/2/2022 22:15",
"initialize" : "jh 7/29/2022 11:36",
"loadProject:" : "lo 8/1/2022 14:22",
"loadProject:" : "mcr 8/2/2022 22:15",
"parseRepoFromUrl:" : "mcr 8/2/2022 21:15",
"removeColumn:" : "lo 8/1/2022 14:09",
"rerenderColumns" : "lo 7/31/2022 16:42",
"resizeColumnArea" : "lo 8/1/2022 11:12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ getRequestToURL: aString
request := self createGetRequestTo: aString.
response := (WebClient new initializeFromUrl: aString) sendRequest: request.
stream := ReadStream on: response content from: 1 to: response content byteSize.

^ Json readFrom: stream.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
request creation/sending
getRequestToURLHeaderOnly: aString

| request response |
request := self createGetRequestTo: aString.
response := (WebClient new initializeFromUrl: aString) sendRequest: request.

^ response headers.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
request creation/sending
getRequestToURLWithoutAuth: aString

| request response stream |
request := self createGetRequestTo: aString.
request removeHeader: 'Authorization'.
response := (WebClient new initializeFromUrl: aString) sendRequest: request.
stream := ReadStream on: response content from: 1 to: response content byteSize.

^ Json readFrom: stream.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
api calls get request
queryScopeHeaderOnly: aString

| url request response |
url := 'https://api.github.com/users/' , aString.
request := self createGetRequestTo: url.
response := (WebClient new initializeFromUrl: url) sendRequest: request.

^ response headersAt: 'X-OAuth-Scopes'.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
api calls get request
queryUser: aString repo: anotherString

| url |
url := 'https://api.github.com/repos/', aString, '/', anotherString.

^ self getRequestToURL: url.
Loading

0 comments on commit 07f17df

Please sign in to comment.