Skip to content

Commit

Permalink
Upgrade library to elm-0-19
Browse files Browse the repository at this point in the history
This doesn't include any breaking change to the API but shuffles a bit
the dependencies (biggest change being the use of elm/url instead of
Bogdanp/QueryString)
  • Loading branch information
KtorZ committed Sep 3, 2018
1 parent 1a5304f commit 3a60354
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 197 deletions.
28 changes: 0 additions & 28 deletions elm-package.json

This file was deleted.

26 changes: 26 additions & 0 deletions elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "package",
"name": "truqu/elm-oauth2",
"version": "1.0.0",
"license": "MIT",
"summary": "OAuth 2.0 client-side utils",
"exposed-modules": [
"OAuth",
"OAuth.AuthorizationCode",
"OAuth.Implicit",
"OAuth.ClientCredentials",
"OAuth.Password",
"OAuth.Decode"
],
"dependencies": {
"elm/url": "1.0.0 <= v < 2.0.0",
"elm/core": "1.0.0 <= v < 2.0.0",
"elm/html": "1.0.0 <= v < 2.0.0",
"elm/http": "1.0.0 <= v < 2.0.0",
"elm/browser": "1.0.0 <= v < 2.0.0",
"elm/json": "1.0.0 <= v < 2.0.0",
"truqu/elm-base64": "2.0.0 <= v < 3.0.0"
},
"test-dependencies": {},
"elm-version": "0.19.0 <= v < 0.20.0"
}
147 changes: 84 additions & 63 deletions src/Internal.elm
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
module Internal exposing (..)
module Internal exposing
( authHeader
, authenticate
, authorize
, makeRequest
, parseAuthorizationCode
, parseError
, parseToken
, qsSpaceSeparatedList
, urlAddList
, urlAddMaybe
)

import Base64
import Browser.Navigation as Navigation
import Http as Http
import OAuth exposing (..)
import OAuth.Decode exposing (..)
import Http as Http
import QueryString as QS
import Navigation as Navigation
import Base64
import Url.Builder as Url exposing (QueryParameter)
import Url.Parser.Query as Query


authorize : Authorization -> Cmd msg
authorize { clientId, url, redirectUri, responseType, scope, state } =
let
qs =
QS.empty
|> QS.add "client_id" clientId
|> QS.add "redirect_uri" redirectUri
|> QS.add "response_type" (showResponseType responseType)
|> qsAddList "scope" scope
|> qsAddMaybe "state" state
|> QS.render
[ Url.string "client_id" clientId
, Url.string "redirect_uri" redirectUri
, Url.string "response_type" (showResponseType responseType)
]
|> urlAddList "scope" scope
|> urlAddMaybe "state" state
|> Url.toQuery
in
Navigation.load (url ++ qs)
Navigation.load (url ++ qs)


authenticate : AdjustRequest ResponseToken -> Authentication -> Http.Request ResponseToken
Expand All @@ -29,56 +41,56 @@ authenticate adjust authentication =
AuthorizationCode { credentials, code, redirectUri, scope, state, url } ->
let
body =
QS.empty
|> QS.add "grant_type" "authorization_code"
|> QS.add "client_id" credentials.clientId
|> QS.add "redirect_uri" redirectUri
|> QS.add "code" code
|> qsAddList "scope" scope
|> qsAddMaybe "state" state
|> QS.render
[ Url.string "grant_type" "authorization_code"
, Url.string "client_id" credentials.clientId
, Url.string "redirect_uri" redirectUri
, Url.string "code" code
]
|> urlAddList "scope" scope
|> urlAddMaybe "state" state
|> Url.toQuery
|> String.dropLeft 1

headers =
authHeader <|
if String.isEmpty credentials.secret then
Nothing

else
Just credentials
in
makeRequest adjust url headers body
makeRequest adjust url headers body

ClientCredentials { credentials, scope, state, url } ->
let
body =
QS.empty
|> QS.add "grant_type" "client_credentials"
|> qsAddList "scope" scope
|> qsAddMaybe "state" state
|> QS.render
[ Url.string "grant_type" "client_credentials" ]
|> urlAddList "scope" scope
|> urlAddMaybe "state" state
|> Url.toQuery
|> String.dropLeft 1

headers =
authHeader (Just { clientId = credentials.clientId, secret = credentials.secret })
in
makeRequest adjust url headers body
makeRequest adjust url headers body

Password { credentials, password, scope, state, url, username } ->
let
body =
QS.empty
|> QS.add "grant_type" "password"
|> QS.add "username" username
|> QS.add "password" password
|> qsAddList "scope" scope
|> qsAddMaybe "state" state
|> QS.render
[ Url.string "grant_type" "password"
, Url.string "username" username
, Url.string "password" password
]
|> urlAddList "scope" scope
|> urlAddMaybe "state" state
|> Url.toQuery
|> String.dropLeft 1

headers =
authHeader credentials
in
makeRequest adjust url headers body
makeRequest adjust url headers body

Refresh { credentials, scope, token, url } ->
let
Expand All @@ -88,17 +100,17 @@ authenticate adjust authentication =
t

body =
QS.empty
|> QS.add "grant_type" "refresh_token"
|> QS.add "refresh_token" refreshToken
|> qsAddList "scope" scope
|> QS.render
[ Url.string "grant_type" "refresh_token"
, Url.string "refresh_token" refreshToken
]
|> urlAddList "scope" scope
|> Url.toQuery
|> String.dropLeft 1

headers =
authHeader credentials
in
makeRequest adjust url headers body
makeRequest adjust url headers body


makeRequest : AdjustRequest ResponseToken -> String -> List Http.Header -> String -> Http.Request ResponseToken
Expand All @@ -114,9 +126,9 @@ makeRequest adjust url headers body =
, withCredentials = False
}
in
requestParts
|> adjust
|> Http.request
requestParts
|> adjust
|> Http.request


authHeader : Maybe Credentials -> List Http.Header
Expand All @@ -140,8 +152,8 @@ parseError error errorDescription errorUri state =

parseToken : String -> Maybe String -> Maybe Int -> List String -> Maybe String -> Result ParseErr ResponseToken
parseToken accessToken mTokenType mExpiresIn scope state =
case ( Maybe.map String.toLower mTokenType, mExpiresIn ) of
( Just "bearer", mExpiresIn ) ->
case Maybe.map String.toLower mTokenType of
Just "bearer" ->
Ok <|
{ expiresIn = mExpiresIn
, refreshToken = Nothing
Expand All @@ -150,10 +162,10 @@ parseToken accessToken mTokenType mExpiresIn scope state =
, token = Bearer accessToken
}

( Just _, _ ) ->
Just _ ->
Result.Err <| Invalid [ "token_type" ]

( Nothing, _ ) ->
Nothing ->
Result.Err <| Missing [ "token_type" ]


Expand All @@ -165,21 +177,30 @@ parseAuthorizationCode code state =
}


qsAddList : String -> List String -> QS.QueryString -> QS.QueryString
qsAddList param xs qs =
case xs of
[] ->
qs
urlAddList : String -> List String -> List QueryParameter -> List QueryParameter
urlAddList param xs qs =
qs
++ (case xs of
[] ->
[]

_ ->
QS.add param (String.join " " xs) qs
_ ->
[ Url.string param (String.join " " xs) ]
)


qsAddMaybe : String -> Maybe String -> QS.QueryString -> QS.QueryString
qsAddMaybe param ms qs =
case ms of
Nothing ->
qs
urlAddMaybe : String -> Maybe String -> List QueryParameter -> List QueryParameter
urlAddMaybe param ms qs =
qs
++ (case ms of
Nothing ->
[]

Just s ->
[ Url.string param s ]
)


Just s ->
QS.add param s qs
qsSpaceSeparatedList : String -> Query.Parser (List String)
qsSpaceSeparatedList param =
Query.map (\s -> Maybe.withDefault "" s |> String.split " ") (Query.string param)
Loading

0 comments on commit 3a60354

Please sign in to comment.