Skip to content

Commit

Permalink
Merge pull request cloudfoundry#221 from jpalermo/master
Browse files Browse the repository at this point in the history
Fix base64 padding
  • Loading branch information
Dan Lavine committed Jul 25, 2014
2 parents 6d4d619 + 2923cd3 commit e2e5938
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 2 additions & 3 deletions cf/configuration/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/base64"
"encoding/json"
"strings"
"unicode/utf8"
)

type TokenInfo struct {
Expand Down Expand Up @@ -47,11 +46,11 @@ func base64Decode(encodedData string) ([]byte, error) {
}

func restorePadding(seg string) string {
switch utf8.RuneCountInString(seg) % 4 {
switch len(seg) % 4 {
case 2:
seg = seg + "=="
case 3:
seg = seg + "==="
seg = seg + "="
}
return seg
}
10 changes: 9 additions & 1 deletion cf/configuration/access_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ var _ = Describe("NewTokenInfo", func() {
Expect(string(decodedInfo)).To(ContainSubstring("user1@example.com"))
})

It("decodes a string into TokenInfo when there is padding present", func() {
It("decodes a string into TokenInfo when there is doubling padding present", func() {
accessToken := "bearer eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiIwNTg2MjlkNC04NjEwLTQ3NTEtOTg3Ny0yOGMwNzE3YTE5ZTciLCJzdWIiOiIzNGFiMDhkOC04YmVmLTQ1MzQtOGYyOC0zODhhYWI1MjAwMmEiLCJzY29wZSI6WyJjbG91ZF9jb250cm9sbGVyLnJlYWQiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwib3BlbmlkIiwicGFzc3dvcmQud3JpdGUiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImdyYW50X3R5cGUiOiJwYXNzd29yZCIsInVzZXJfaWQiOiIzNGFiMDhkOC04YmVmLTQ1MzQtOGYyOC0zODhhYWI1MjAwMmEiLCJ1c2VyX25hbWUiOiJ0bGFuZ0Bnb3Bpdm90YWwuY29tIiwiZW1haWwiOiJ0bGFuZ0Bnb3Bpdm90YWwuY29tIiwiaWF0IjoxMzc3MDk1ODM5LCJleHAiOjEzNzcxMzkwMzksImlzcyI6Imh0dHBzOi8vdWFhLnJ1bi5waXZvdGFsLmlvL29hdXRoL3Rva2VuIiwiYXVkIjpbIm9wZW5pZCIsImNsb3VkX2NvbnRyb2xsZXIiLCJwYXNzd29yZCJdfQ.dcgrGjPvTjYvg8dTSZY5ecZZTNt59IYd442VaEXXvLNB_WQCAdbVOxiJ14ogzQkkzDDw60Q2lbw4z6HrqM1a-BNpYfRmvaIP_79GpIZC6OzQy_PgA1whL27pO7_ABkSJT1CEgJQJMTQlYOiZNHvFTWen3G4O6ey680cxIN5VvbFjmmQHCuwANE9_GqnYYvoI9tS1nERku8DX2H9KH5NAgDa52-p0NhLnZRqYjGss6EyPYkwYN5w2OizfYUmEYVWo8K1Q45_TGMoE-LgZe2mGWwv0euLYBoFTkYhtBMj91dQagLrL1aGcmDKPc6ivkXtfpN4Zv7FJ9OXJ2DPQyHKRpw"
decodedInfo, err := DecodeAccessToken(accessToken)

Expect(err).NotTo(HaveOccurred())
Expect(string(decodedInfo)).To(ContainSubstring("tlang@gopivotal.com"))
})

It("decodes a string into TokenInfo when there is single padding present", func() {
accessToken := "bearer eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiIwNTg2MjlkNC04NjEwLTQ3NTEtOTg3Ny0yOGMwNzE3YTE5ZTciLCJzdWIiOiIzNGFiMDhkOC04YmVmLTQ1MzQtOGYyOC0zODhhYWI1MjAwMmEiLCJzY29wZSI6WyJjbG91ZF9jb250cm9sbGVyLnJlYWQiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwib3BlbmlkIiwicGFzc3dvcmQud3JpdGUiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImdyYW50X3R5cGUiOiJwYXNzd29yZCIsInVzZXJfaWQiOiIzNGFiMDhkOC04YmVmLTQ1MzQtOGYyOC0zODhhYWI1MjAwMmEiLCJ1c2VyX25hbWUiOiJ0bGFuZzFAZ29waXZvdGFsLmNvbSIsImVtYWlsIjoidGxhbmdAZ29waXZvdGFsLmNvbSIsImlhdCI6MTM3NzA5NTgzOSwiZXhwIjoxMzc3MTM5MDM5LCJpc3MiOiJodHRwczovL3VhYS5ydW4ucGl2b3RhbC5pby9vYXV0aC90b2tlbiIsImF1ZCI6WyJvcGVuaWQiLCJjbG91ZF9jb250cm9sbGVyIiwicGFzc3dvcmQiXX0.dcgrGjPvTjYvg8dTSZY5ecZZTNt59IYd442VaEXXvLNB_WQCAdbVOxiJ14ogzQkkzDDw60Q2lbw4z6HrqM1a-BNpYfRmvaIP_79GpIZC6OzQy_PgA1whL27pO7_ABkSJT1CEgJQJMTQlYOiZNHvFTWen3G4O6ey680cxIN5VvbFjmmQHCuwANE9_GqnYYvoI9tS1nERku8DX2H9KH5NAgDa52-p0NhLnZRqYjGss6EyPYkwYN5w2OizfYUmEYVWo8K1Q45_TGMoE-LgZe2mGWwv0euLYBoFTkYhtBMj91dQagLrL1aGcmDKPc6ivkXtfpN4Zv7FJ9OXJ2DPQyHKRpw"
decodedInfo, err := DecodeAccessToken(accessToken)

Expect(err).NotTo(HaveOccurred())
Expect(string(decodedInfo)).To(ContainSubstring("tlang1@gopivotal.com"))
})
})

0 comments on commit e2e5938

Please sign in to comment.