Skip to content

Commit

Permalink
Finish linting fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
didrocks committed Aug 9, 2023
1 parent b3daba2 commit 0650fdd
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 28 deletions.
16 changes: 13 additions & 3 deletions pam/authmodeselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func (m authModeSelectionModel) Update(msg tea.Msg) (authModeSelectionModel, tea

// Select correct line to ensure model is synchronised
for i, a := range m.Items() {
if a.(authModeItem).id != msg.id {
a := listItemsToAuthModeItem(a)
if a.id != msg.id {
continue
}
m.Select(i)
Expand All @@ -114,7 +115,7 @@ func (m authModeSelectionModel) Update(msg tea.Msg) (authModeSelectionModel, tea
if item == nil {
return m, nil
}
authMode := item.(authModeItem)
authMode := listItemsToAuthModeItem(item)
cmd := selectAuthMode(authMode.id)
return m, cmd
case "1", "2", "3", "4", "5", "6", "7", "8", "9":
Expand All @@ -125,7 +126,7 @@ func (m authModeSelectionModel) Update(msg tea.Msg) (authModeSelectionModel, tea
return m, nil
}
item := items[choice-1]
authMode := item.(authModeItem)
authMode := listItemsToAuthModeItem(item)
cmd := selectAuthMode(authMode.id)
return m, cmd
}
Expand Down Expand Up @@ -230,3 +231,12 @@ func getAuthenticationModes(client authd.PAMClient, sessionID string) tea.Cmd {
func (m *authModeSelectionModel) Reset() {
m.currentAuthModeSelectedID = ""
}

// listItemsToAuthModeItem panics if item is not an authModeItem (programming error).
func listItemsToAuthModeItem(item list.Item) authModeItem {
r, ok := item.(authModeItem)
if !ok {
panic(fmt.Sprintf("expected authModeItem, got %T", r))
}
return r
}
9 changes: 7 additions & 2 deletions pam/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func (m *authorizationModel) Init() tea.Cmd {
// Update handles events and actions.
func (m *authorizationModel) Update(msg tea.Msg) (authorizationModel, tea.Cmd) {
switch msg := msg.(type) {

case reselectAuthMode:
m.cancelIsAuthorized()
return *m, sendEvent(AuthModeSelected{})
Expand Down Expand Up @@ -143,7 +142,11 @@ func (m *authorizationModel) Update(msg tea.Msg) (authorizationModel, tea.Cmd) {
var model tea.Model
if m.currentModel != nil {
model, cmd = m.currentModel.Update(msg)
m.currentModel = model.(authorizationComponent)
c, ok := model.(authorizationComponent)
if !ok {
panic(fmt.Sprintf("expected authorizationComponent, got %T", c))
}
m.currentModel = c
}
return *m, cmd
}
Expand Down Expand Up @@ -240,6 +243,8 @@ func dataToMsg(data string) string {
}

// dataToUserInfo returns the user information from a given JSON string.
//
//nolint:unused // This is not used for now TODO
func dataToUserInfo(data string) string {
/*v := make(map[string]string)
if err := json.Unmarshal([]byte(data), &v); err != nil {
Expand Down
24 changes: 18 additions & 6 deletions pam/brokerselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func (m brokerSelectionModel) Update(msg tea.Msg) (brokerSelectionModel, tea.Cmd
}
// Select correct line to ensure model is synchronised
for i, b := range m.Items() {
if b.(brokerItem).id != broker.Id {
b := listItemsToBrokerItem(b)
if b.id != broker.Id {
continue
}
m.Select(i)
Expand All @@ -116,7 +117,7 @@ func (m brokerSelectionModel) Update(msg tea.Msg) (brokerSelectionModel, tea.Cmd
if item == nil {
return m, nil
}
broker := item.(brokerItem)
broker := listItemsToBrokerItem(item)
cmd := selectBroker(broker.id)
return m, cmd
case "1", "2", "3", "4", "5", "6", "7", "8", "9":
Expand All @@ -127,7 +128,7 @@ func (m brokerSelectionModel) Update(msg tea.Msg) (brokerSelectionModel, tea.Cmd
return m, nil
}
item := items[choice-1]
broker := item.(brokerItem)
broker := listItemsToBrokerItem(item)
cmd := selectBroker(broker.id)
return m, cmd
}
Expand All @@ -138,7 +139,9 @@ func (m brokerSelectionModel) Update(msg tea.Msg) (brokerSelectionModel, tea.Cmd
return m, cmd
}

// Focus focuses this model.
// Focus focuses this model. It always returns nil.
//
//nolint:unparam // Always returns nil.
func (m *brokerSelectionModel) Focus() tea.Cmd {
m.focused = true
return nil
Expand Down Expand Up @@ -183,7 +186,7 @@ func (m brokerSelectionModel) WillCaptureEscape() bool {
return m.FilterState() == list.Filtering
}

// brokerItem
// brokerItem is the list item corresponding to a broker.
type brokerItem struct {
id string
name string
Expand All @@ -206,7 +209,6 @@ func (d itemLayout) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }

// Render writes to w the rendering of the items based on its selection and type.
func (d itemLayout) Render(w io.Writer, m list.Model, index int, item list.Item) {

var label string
switch item := item.(type) {
case brokerItem:
Expand Down Expand Up @@ -258,3 +260,13 @@ func brokerFromID(brokerID string, brokers []*authd.ABResponse_BrokerInfo) *auth
}
return nil
}

// listItemsToBrokerItem panics if item is not an brokerItem (programming error).
// TODO: transform to generic?
func listItemsToBrokerItem(item list.Item) brokerItem {
r, ok := item.(brokerItem)
if !ok {
panic(fmt.Sprintf("expected brokerItem, got %T", r))
}
return r
}
7 changes: 6 additions & 1 deletion pam/formmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ func (m formModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
var newModel tea.Model
newModel, cmd = fm.Update(msg)
m.focusableModels[i] = newModel.(authorizationComponent)
// it should be a authorizationComponent, otherwise it’s a programming error.
c, ok := newModel.(authorizationComponent)
if !ok {
panic(fmt.Sprintf("expected authorizationComponent, got %T", c))
}
m.focusableModels[i] = c
}

return m, cmd
Expand Down
4 changes: 2 additions & 2 deletions pam/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

var debug string

// state represents the stage object
// state represents the stage object.
type stage int

const (
Expand Down Expand Up @@ -47,7 +47,7 @@ type model struct {
authModeSelectionModel authModeSelectionModel
authorizationModel authorizationModel

exitMsg error
exitMsg ExitMsger
}

/* global events */
Expand Down
8 changes: 4 additions & 4 deletions pam/pam.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ func pam_sm_authenticate(pamh *C.pam_handle_t, flags, argc C.int, argv **C.char)
case pamSuccess:
return C.PAM_SUCCESS
case pamIgnore:
if exitMsg.Error() != "" {
if exitMsg.ExitMsg() != "" {
log.Debugf(context.TODO(), "Ignoring authd authentication: %v", exitMsg)
}
logErrMsg = ""
errCode = C.PAM_IGNORE
case pamAbort:
if exitMsg.Error() != "" {
if exitMsg.ExitMsg() != "" {
logErrMsg = fmt.Sprintf("cancelled authentication: %v", exitMsg)
}
errCode = C.PAM_ABORT
case pamAuthError:
if exitMsg.Error() != "" {
if exitMsg.ExitMsg() != "" {
logErrMsg = fmt.Sprintf("authentication: %v", exitMsg)
}
errCode = C.PAM_AUTH_ERR
case pamSystemError:
if exitMsg.Error() != "" {
if exitMsg.ExitMsg() != "" {
logErrMsg = fmt.Sprintf("system: %v", exitMsg)
}
errCode = C.PAM_SYSTEM_ERR
Expand Down
26 changes: 16 additions & 10 deletions pam/return.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package main

// Various signalling return messaging to PAM.

// ExitMsger is the exit message type and optional accompagning details.
type ExitMsger interface {
ExitMsg() string
}

// pamSuccess signals PAM module to return PAM_SUCCESS and Quit tea.Model.
type pamSuccess struct {
}

func (err pamSuccess) Error() string {
// ExitMsg returns the string of pamSuccess.
func (err pamSuccess) ExitMsg() string {
return ""
}

Expand All @@ -15,8 +21,8 @@ type pamIgnore struct {
msg string
}

// Error returns the string of pamIgnore message.
func (err pamIgnore) Error() string {
// ExitMsg returns the string of pamIgnore message.
func (err pamIgnore) ExitMsg() string {
return err.msg
}

Expand All @@ -25,18 +31,18 @@ type pamAbort struct {
msg string
}

// Error returns the string of pamAbort message.
func (err pamAbort) Error() string {
// ExitMsg returns the string of pamAbort message.
func (err pamAbort) ExitMsg() string {
return err.msg
}

// pamSystemError signals PAM module to return PAM_SYSTEM_ERROR and Quit tea.Model
// pamSystemError signals PAM module to return PAM_SYSTEM_ERROR and Quit tea.Model.
type pamSystemError struct {
msg string
}

// Error returns the string of pamSystemError message.
func (err pamSystemError) Error() string {
// ExitMsg returns the string of pamSystemError message.
func (err pamSystemError) ExitMsg() string {
return err.msg
}

Expand All @@ -45,7 +51,7 @@ type pamAuthError struct {
msg string
}

// Error returns the string of pamAuthError message.
func (err pamAuthError) Error() string {
// ExitMsg returns the string of pamAuthError message.
func (err pamAuthError) ExitMsg() string {
return err.msg
}

0 comments on commit 0650fdd

Please sign in to comment.