• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Waffle / waffle / 6364

01 Feb 2026 02:07AM UTC coverage: 46.217%. Remained the same
6364

push

github

web-flow
Merge pull request #3206 from Waffle/renovate/checkstyle.version

Update dependency com.puppycrawl.tools:checkstyle to v13.1.0

276 of 734 branches covered (37.6%)

Branch coverage included in aggregate %.

1019 of 2068 relevant lines covered (49.27%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.71
/Source/JNA/waffle-jna/src/main/java/waffle/util/AuthorizationHeader.java
1
/*
2
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
6
 */
7
package waffle.util;
8

9
import java.util.Base64;
10
import java.util.Locale;
11

12
import javax.servlet.http.HttpServletRequest;
13

14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16

17
/**
18
 * Authorization header.
19
 */
20
public class AuthorizationHeader {
21

22
    /** The logger. */
23
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationHeader.class);
2✔
24

25
    /** The request. */
26
    private final HttpServletRequest request;
27

28
    /**
29
     * Instantiates a new authorization header.
30
     *
31
     * @param httpServletRequest
32
     *            the http servlet request
33
     */
34
    public AuthorizationHeader(final HttpServletRequest httpServletRequest) {
2✔
35
        this.request = httpServletRequest;
2✔
36
    }
2✔
37

38
    /**
39
     * Gets the header.
40
     *
41
     * @return the header
42
     */
43
    public String getHeader() {
44
        return this.request.getHeader("Authorization");
2✔
45
    }
46

47
    /**
48
     * Checks if is null.
49
     *
50
     * @return true, if is null
51
     */
52
    public boolean isNull() {
53
        return this.getHeader() == null || this.getHeader().length() == 0;
2!
54
    }
55

56
    /**
57
     * Returns a supported security package string.
58
     *
59
     * <pre>
60
     * Authorization: NTLM the_token
61
     * Authorization: Negotiate the_token
62
     * Authorization: Bearer the_token
63
     * </pre>
64
     *
65
     * @return AuthenticationScheme as SecurityPackage e.g. Negotiate, NTLM, Bearer.
66
     */
67
    public String getSecurityPackage() {
68
        final String header = this.getHeader();
2✔
69

70
        if (header == null) {
2!
71
            throw new RuntimeException("Missing Authorization: header");
×
72
        }
73

74
        final int space = header.indexOf(' ');
2✔
75
        if (space > 0) {
2!
76
            return header.substring(0, space);
2✔
77
        }
78

79
        throw new RuntimeException("Invalid Authorization header: " + header);
×
80
    }
81

82
    @Override
83
    public String toString() {
84
        return this.isNull() ? "<none>" : this.getHeader();
×
85
    }
86

87
    /**
88
     * Gets the token.
89
     *
90
     * @return the token
91
     */
92
    public String getToken() {
93
        return this.getHeader().substring(this.getSecurityPackage().length() + 1);
×
94
    }
95

96
    /**
97
     * Gets the token bytes.
98
     *
99
     * @return the token bytes
100
     */
101
    public byte[] getTokenBytes() {
102
        try {
103
            return Base64.getDecoder().decode(this.getToken());
×
104
        } catch (final IllegalArgumentException e) {
×
105
            AuthorizationHeader.LOGGER.debug("", e);
×
106
            throw new RuntimeException("Invalid authorization header.");
×
107
        }
108
    }
109

110
    /**
111
     * Checks if is ntlm type1 message.
112
     *
113
     * @return true, if is ntlm type1 message
114
     */
115
    public boolean isNtlmType1Message() {
116
        if (this.isNull()) {
×
117
            return false;
×
118
        }
119

120
        final byte[] tokenBytes = this.getTokenBytes();
×
121
        if (!NtlmMessage.isNtlmMessage(tokenBytes)) {
×
122
            return false;
×
123
        }
124

125
        return 1 == NtlmMessage.getMessageType(tokenBytes);
×
126
    }
127

128
    /**
129
     * Checks if is SP nego message.
130
     *
131
     * @return true, if is SP nego message that contains NegTokenInit
132
     */
133
    public boolean isSPNegTokenInitMessage() {
134

135
        if (this.isNull()) {
×
136
            return false;
×
137
        }
138

139
        final byte[] tokenBytes = this.getTokenBytes();
×
140
        return SPNegoMessage.isNegTokenInit(tokenBytes);
×
141
    }
142

143
    /**
144
     * When using NTLM authentication and the browser is making a POST request, it preemptively sends a Type 2
145
     * authentication message (without the POSTed data). The server responds with a 401, and the browser sends a Type 3
146
     * request with the POSTed data. This is to avoid the situation where user's credentials might be potentially
147
     * invalid, and all this data is being POSTed across the wire.
148
     *
149
     * @return True if request is an NTLM POST, PUT, or DELETE with an Authorization header and no data.
150
     */
151
    public boolean isNtlmType1PostAuthorizationHeader() {
152
        if (!"POST".equals(this.request.getMethod()) && !"PUT".equals(this.request.getMethod())
×
153
                && !"DELETE".equals(this.request.getMethod())) {
×
154
            return false;
×
155
        }
156

157
        if (this.request.getContentLength() != 0) {
×
158
            return false;
×
159
        }
160

161
        return this.isNtlmType1Message() || this.isSPNegTokenInitMessage();
×
162
    }
163

164
    /**
165
     * Is Bearer Authorization Header will return true if 'BEARER' exists.
166
     *
167
     * @return True if header contains 'BEARER' header.
168
     */
169
    public boolean isBearerAuthorizationHeader() {
170
        if (this.isNull()) {
2!
171
            return false;
×
172
        }
173

174
        return this.getSecurityPackage().toUpperCase(Locale.ENGLISH).equalsIgnoreCase("BEARER");
2✔
175
    }
176
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc