generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from Avanade/sprint-36
Sprint 36
- Loading branch information
Showing
53 changed files
with
907 additions
and
1,455 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/goapp/controller/authentication/authentication-controller-interface.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package authentication | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type AuthenticationPageController interface { | ||
CallbackHandler(w http.ResponseWriter, r *http.Request) | ||
LoginHandler(w http.ResponseWriter, r *http.Request) | ||
LoginRedirectHandler(w http.ResponseWriter, r *http.Request) | ||
LogoutHandler(w http.ResponseWriter, r *http.Request) | ||
} |
116 changes: 116 additions & 0 deletions
116
src/goapp/controller/authentication/authentication-page-controller.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package authentication | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
"html/template" | ||
"main/service" | ||
"net/http" | ||
) | ||
|
||
type authenticationPageController struct { | ||
*service.Service | ||
} | ||
|
||
func NewAuthenticationController(s *service.Service) AuthenticationPageController { | ||
return &authenticationPageController{ | ||
Service: s, | ||
} | ||
} | ||
|
||
func (a *authenticationPageController) CallbackHandler(w http.ResponseWriter, r *http.Request) { | ||
state, err := a.Authenticator.GetStringValue(r, "auth-session", "state") | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if r.URL.Query().Get("state") != state { | ||
http.Error(w, "Invalid state parameter", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
//Retrieve token and save data on session store | ||
u, err := a.Authenticator.ProcessToken(r.URL.Query().Get("code")) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
data := map[string]interface{}{ | ||
"id_token": u.IdToken, | ||
"access": u.AccessToken, | ||
"profile": u.Profile, | ||
"refresh_token": u.RefreshToken, | ||
"expiry": u.Expiry, | ||
} | ||
|
||
err = a.Authenticator.SaveOnSession(&w, r, "auth-session", data) | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Redirect to index | ||
http.Redirect(w, r, "/", http.StatusSeeOther) | ||
} | ||
|
||
func (a *authenticationPageController) LoginHandler(w http.ResponseWriter, r *http.Request) { | ||
// Generate random state | ||
b := make([]byte, 32) | ||
_, err := rand.Read(b) | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
state := base64.StdEncoding.EncodeToString(b) | ||
|
||
data := map[string]interface{}{ | ||
"state": state, | ||
} | ||
|
||
err = a.Authenticator.SaveOnSession(&w, r, "auth-session", data) | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
http.Redirect(w, r, a.Authenticator.GetAuthCodeURL(state), http.StatusTemporaryRedirect) | ||
} | ||
|
||
func (a *authenticationPageController) LoginRedirectHandler(w http.ResponseWriter, r *http.Request) { | ||
q := r.URL.Query() | ||
redirect := "/" | ||
if len(q["redirect"]) > 0 { | ||
redirect = q["redirect"][0] | ||
} | ||
data := map[string]interface{}{ | ||
"redirect": redirect, | ||
} | ||
|
||
c := http.Cookie{ | ||
Name: "auth-session", | ||
MaxAge: -1} | ||
http.SetCookie(w, &c) | ||
|
||
tmpl := template.Must(template.ParseFiles("templates/loginredirect.html")) | ||
tmpl.Execute(w, data) | ||
} | ||
|
||
func (a *authenticationPageController) LogoutHandler(w http.ResponseWriter, r *http.Request) { | ||
err := a.Authenticator.ClearFromSession(&w, r, "auth-session") | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
|
||
url, err := a.Authenticator.GetLogoutURL() | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
http.Redirect(w, r, url, http.StatusTemporaryRedirect) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/goapp/controller/fallback/fallback-controller-interface.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package fallback | ||
|
||
import "net/http" | ||
|
||
type FallbackController interface { | ||
NotFound(w http.ResponseWriter, r *http.Request) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package fallback | ||
|
||
import ( | ||
"main/service" | ||
"net/http" | ||
) | ||
|
||
type fallbackController struct { | ||
*service.Service | ||
} | ||
|
||
func NewFallbackController(s *service.Service) FallbackController { | ||
return &fallbackController{ | ||
Service: s, | ||
} | ||
} | ||
|
||
func (c *fallbackController) NotFound(w http.ResponseWriter, r *http.Request) { | ||
user, err := c.Authenticator.GetAuthenticatedUser(r) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
t, d := c.Service.Template.UseTemplate("NotFound", r.URL.Path, *user, nil) | ||
err = t.Execute(w, d) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} |
Oops, something went wrong.